duanjianxu4288 2014-12-19 06:45
浏览 1572
已采纳

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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器