Reputation: 1
I'm trying to get data from mySQL database on my machine. When i rum the app on the emulator i get a HttpHostConnectException
(i have tested my address in a browser and tried 10.0.2.2 with and without the port number) then the app crashes with NullPointerException
. What can I do fix these issue?
Connector.java
public class Connector {
public JSONArray getResults(){
String url = "http://192.168.87.1/android/getStudents.php";
HttpEntity httpentity = null;
try
{
DefaultHttpClient httpclient =new DefaultHttpClient();
HttpGet httpget= new HttpGet(url);
HttpResponse httpresponse = httpclient.execute(httpget);
httpentity = httpresponse.getEntity();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
JSONArray jsonarray = null;
if(httpentity != null){
try{
String entityresponse = EntityUtils.toString(httpentity);
Log.e("Entity Response: ", entityresponse);
jsonarray = new JSONArray(entityresponse);
}catch (JSONException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
return jsonarray;
}}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private TextView resultsViewer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.resultsViewer = (TextView)this.findViewById(R.id.resultsViewer);
new getResultsTask().execute(new Connector());
}
private void setText (JSONArray jsonarray){
String string = "";
for(int i=0; i<jsonarray.length(); i++){
JSONObject json = null;
try{
json = jsonarray.getJSONObject(i);
string = string + "Exam : " + json.getString("exam_name") + "\n" +
"Score :" + json.getString("score") + "\n\n";
}catch (JSONException e){
e.printStackTrace();
}
}
this.resultsViewer.setText(string);
}
private class getResultsTask extends AsyncTask<Connector, Long, JSONArray>{
@Override
protected JSONArray doInBackground(Connector... params) {
return params[0].getResults();
}
@Override
protected void onPostExecute(JSONArray jsonarray) {
setText(jsonarray);
}
}}
some lines from LogCat
Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f09003f}
Emulator without GPU emulation detected.
Background sticky concurrent mark sweep GC freed 3259(270KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 759KB/1117KB, paused 3.009ms total 1.004s
org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.56.1 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:188)
and the NullPointerExeption
Shutting down VM
--------- beginning of crash
FATAL EXCEPTION: main
Process: com.ed.jsontest2, PID: 1302
java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
at com.ed.jsontest2.MainActivity.setText(MainActivity.java:32)
Upvotes: 0
Views: 327
Reputation: 1255
you have to use the ip address 10.0.0.2 , if the server is running in your local machine.the port number is necessary
your url should be http://10.0.2.2:portnumber/android/getStudents.php...
port number is your local server running port
if you are running the server from remote machin then replace the host ipaddress
Upvotes: 0