gpt4 book ai didi

java - Android Studio 错误 org.apache.http.conn.HttpHostConnectException : Connection to http://10. 0.2.2:8080 拒绝

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:24 44 4
gpt4 key购买 nike

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

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 很陌生,所以请解释一下解决方案。非常感谢您的帮助。

最佳答案

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

关于java - Android Studio 错误 org.apache.http.conn.HttpHostConnectException : Connection to http://10. 0.2.2:8080 拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560963/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com