2014-12-19 141 views
1

我的Android Studio帶有wamp服務器。我正在嘗試使用php文件訪問phpmyadmin上的MySql數據庫。我已經嘗試過模擬器和Android設備。我總是收到此錯誤:Android Studio錯誤org.apache.http.conn.HttpHostConnectException:連接到http://10.0.2.2:8080拒絕

org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused 

這是我MainActivity.jav文件

public class MainActivity extends ActionBarActivity { 

private TextView responseTView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    this.responseTView = (TextView) findViewById(R.id.response); 

    new getDetails().execute(new SqlConnector()); 
} 

private void setTextToTextView(JSONArray jsonArray) { 
    String s = ""; 
    for(int i=0; i<jsonArray.length();i++) { 
     JSONObject obj = null; 
     try { 
      obj = jsonArray.getJSONObject(i); 
      s = s + "ID: " + obj.getString("id") + "Customer Name:" + obj.getString("Customer"); 
     } 
     catch(JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
    this.responseTView.setText(s); 
} 

private class getDetails extends AsyncTask<SqlConnector,Long,JSONArray> { 

    @Override 
    protected JSONArray doInBackground(SqlConnector... params) { 
     return params[0].getDetailsinJson(); 
    } 
    @Override 
    protected void onPostExecute(JSONArray jsonArray) { 
    setTextToTextView(jsonArray); 
    } 
} 

}

這是我SqlConnector.java文件

public class SqlConnector { 
public JSONArray getDetailsinJson() { 
    String url = "http://10.0.2.2:8080/D:/Database/main1.php"; 

    HttpEntity httpEntity = null; 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     Log.d("This is inside HTTP try block.","No error.."); 
     HttpResponse httpResponse = httpClient.execute(httpGet); //Using Logs, it's this line that brings on the error. That's my understanding. 

     httpEntity=httpResponse.getEntity(); 

    } 
    catch(ClientProtocolException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Log.d("Inside SqlConnector Class", "ajsdhlk"); 
    JSONArray jsonArray = null; 
    if(httpEntity != null) { 
     try { 
      String response= EntityUtils.toString(httpEntity); 
      Log.e("This is the response", response); 
      jsonArray = new JSONArray(response); 
     } 
     catch(JSONException e) { 
      Log.d("This is inside Catch block.","Error in JSONArray"); 
      e.printStackTrace(); 
     } 
     catch(IOException e) { 
      Log.d("This is inside catch block.","IOException"); 
      e.printStackTrace(); 
      } 
    } 
    return jsonArray; 
} 

}

I在類似問題上閱讀了很多關於Stackoverflow的文章。但是,我嘗試了很多事情後無法通過這個錯誤。我試圖改變我使用的IP,但它沒有幫助。請幫忙。 我對Android Studio相當陌生,請解釋一下解決方案。非常感謝您的幫助。

回答

0

嘗試了很多選項數小時後。我改變了我的服務器。我用一個在線網站託管網站來存儲我的文件。而且,這不知何故阻止了這個錯誤。如果你認爲還有其他出路,請使用wamp,然後請讓我知道。

相關問題