duanjianxu4288
2014-12-19 06:45
浏览 1.5k

Android Studio错误org.apache.http.conn.HttpHostConnectException:拒绝连接到http://10.0.2.2:8080

I have Android Studio with wamp server. I am trying to use a php file to access MySql database on phpmyadmin. I have tried the emulator and Android Device. I am always getting this error :

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

This is my MainActivity.jav file

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);
    }
}

}

And this is my SqlConnector.java file

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 have read many posts on Stackoverflow over similar problems. But, I am not able to get past this error after trying many things. I tried changing the IP I have used but it doesn't help. Please help. I am quite new to Android Studio, so please explain the solutions. Thanks a lot for help.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyixiu3110 2014-12-22 16:39
    已采纳

    After trying lot of options for hours. I changed my server. I used an online web hosting site to store my files. And, this somehow stopped this error. If you think there is some other way out, to use wamp then please do let me know.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题