2016-04-25 111 views
0

點擊登錄按鈕「嘗試登錄」消息後只顯示,應用程序中沒有進一步的活動,並且在單擊該按鈕後,LOGCAT中顯示異常「java.net.ConnectException :無法連接到/127.0.0.1(端口80)15000ms後:isConnected失敗:ECONNREFUSED(連接被拒絕)「。可能只有先行正在運行,而不是後臺tasks.But問題在哪裏?無法連接到端口80

Login.java --->

public class Login extends Activity { 
     AutoCompleteTextView UsernameEt; 
     EditText PasswordEt; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_login); 
      UsernameEt = (AutoCompleteTextView) findViewById(R.id.email); 
      PasswordEt = (EditText) findViewById(R.id.password); 
     } 

     class PostAsync extends AsyncTask<String, String, JSONObject> { 
      JSONParser jsonParser = new JSONParser(); 

      private ProgressDialog pDialog; 

      // private static final String LOGIN_URL = "http://10.0.3.2:8080/login3.php"; 
      // private static final String LOGIN_URL = "http://192.168.*.*/login3.php"; 
      private static final String LOGIN_URL = "http://127.0.0.1/login3.php"; 

      private static final String TAG_SUCCESS = "success"; 
      private static final String TAG_MESSAGE = "message"; 


      @Override 
      protected void onPreExecute() { 
       pDialog = new ProgressDialog(Login.this); 
       pDialog.setMessage("Attempting login..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 
       pDialog.show(); 
      } 

      @Override 
      protected JSONObject doInBackground(String... args) { 

       try { 

        HashMap<String, String> params = new HashMap<>(); 
        params.put("name", args[0]); 
        params.put("password", args[1]); 

        Log.d("request", "starting"); 

        JSONObject json = jsonParser.makeHttpRequest(
          LOGIN_URL, "POST", params); 

        if (json != null) { 
         Log.d("JSON result", json.toString()); 

         return json; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       return null; 
      } 

      protected void onPostExecute(JSONObject json) { 

       int success = 0; 
       String message = ""; 

       if (pDialog != null && pDialog.isShowing()) { 
        pDialog.dismiss(); 
       } 

       if (json != null) { 
        Toast.makeText(Login.this, json.toString(), 
          Toast.LENGTH_LONG).show(); 

        try { 
         success = json.getInt(TAG_SUCCESS); 
         message = json.getString(TAG_MESSAGE); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 

       if (success == 1) { 
        Log.d("Success!", message); 
       }else{ 
        Log.d("Failure", message); 
       } 
      } 

     } 

     public void OnLogin(View view) { 
      String username = UsernameEt.getText().toString(); 
      String password = PasswordEt.getText().toString(); 

      new PostAsync().execute(username, password); 

     } 
    } 

................................ ................................................ JsonParser的.java ---->

public class JSONParser { 

    String charset = "UTF-8"; 
    HttpURLConnection conn; 
    DataOutputStream wr; 
    StringBuilder result; 
    URL urlObj; 
    JSONObject jObj = null; 
    StringBuilder sbParams; 
    String paramsString; 

    public JSONObject makeHttpRequest(String url, String method, 
             HashMap<String, String> params) { 

     sbParams = new StringBuilder(); 
     int i = 0; 
     for (String key : params.keySet()) { 
      try { 
       if (i != 0){ 
        sbParams.append("&"); 
       } 
       sbParams.append(key).append("=") 
         .append(URLEncoder.encode(params.get(key), charset)); 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
      i++; 
     } 

     if (method.equals("POST")) { 
      // request method is POST 
      try { 
       urlObj = new URL(url); 

       conn = (HttpURLConnection) urlObj.openConnection(); 

       conn.setDoOutput(true); 

       conn.setRequestMethod("POST"); 

       conn.setRequestProperty("Accept-Charset", charset); 

       conn.setReadTimeout(10000); 
       conn.setConnectTimeout(15000); 

       conn.connect(); 

       paramsString = sbParams.toString(); 

       wr = new DataOutputStream(conn.getOutputStream()); 
       wr.writeBytes(paramsString); 
       wr.flush(); 
       wr.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     else if(method.equals("GET")){ 
      // request method is GET 

      if (sbParams.length() != 0) { 
       url += "?" + sbParams.toString(); 
      } 

      try { 
       urlObj = new URL(url); 

       conn = (HttpURLConnection) urlObj.openConnection(); 

       conn.setDoOutput(false); 

       conn.setRequestMethod("GET"); 

       conn.setRequestProperty("Accept-Charset", charset); 

       conn.setConnectTimeout(15000); 

       conn.connect(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

     try { 
      //Receive the response from the server 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
      result = new StringBuilder(); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       result.append(line); 
      } 

      Log.d("JSON Parser", "result: " + result.toString()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     conn.disconnect(); 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(result.toString()); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON Object 
     return jObj; 
    } 
} 
+0

你試圖連接到您的手機上的本地主機端口80或你想從電腦模擬器訪問數據正在運行? – Emmanuel

+0

我在genymotion上運行它。我已經嘗試過「http://10.0.3.2:8080/login3.php」; .......... 「http://192.168.*.*/ login3.php「; ......... 」http://127.0.0.1/login3.php「; –

+0

你在主機上運行Apache或者其他東西嗎? – Emmanuel

回答

-1

使用10.0.2.2而不是127.0.0.1或本地主機

相關問題