【问题标题】:Android Studio error org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refusedAndroid Studio 错误 org.apache.http.conn.HttpHostConnectException:连接到 http://10.0.2.2:8080 被拒绝
【发布时间】:2015-05-16 03:01:55
【问题描述】:

我有带有 wamp 服务器的 Android Studio。我正在尝试使用 php 文件访问 phpmyadmin 上的 MySql 数据库。我试过模拟器和安卓设备。我总是收到这个错误:

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

}

我在 Stackoverflow 上阅读了很多关于类似问题的帖子。但是,在尝试了很多事情之后,我无法克服这个错误。我尝试更改我使用的 IP,但它没有帮助。请帮忙。 我对 Android Studio 很陌生,所以请解释一下解决方案。非常感谢您的帮助。

【问题讨论】:

    标签: java php android


    【解决方案1】:

    在尝试了几个小时的许多选项之后。我改变了我的服务器。我使用在线虚拟主机站点来存储我的文件。而且,这以某种方式阻止了这个错误。如果您认为还有其他方法可以使用 wamp,请告诉我。

    【讨论】:

      【解决方案2】:

      我设法通过将我的 IP 地址从 cmd -> ipconfig 写入 url 来解决这个问题:

      字符串 url = "http://192.168.x.x/D:/Database/main1.php";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多