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.