I am working on an Android App and I am stuck on an error. I am attempting to communicate with a server and get a JSON object from that server. I made a class called ServerCommunication which extends AsynchTask. My server communication class has the standard doInBackground() method. In this method, I try to parse a JSON object from a webpage but an exception is thrown in that method.
I should mention im running my server locally on my machine for now. Therefore, the url im passing my ServerCommunication is: http://127.0.0.1:9000/getVehicle/2942
. This webpage contains plain text of a JSON object. Ill include the JSON object at the bottom.
The exception is: java.lang.NullPointerException: lock == null
. This exception is thrown in the second try-catch.
The line that causes this exception is:
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
This is line has an error because my InputStream is
is null. For some reason, this line is throwing an exception which is why is
is null. Ill include my log output for this at the end of this post
HttpResponse httpResponse = httpClient.execute(httpPost);
Here is my ServerCommunication class:
class ServerCommunication extends AsyncTask<String, Integer, JSONObject> {
public ServerResponse delegate = null;
public JSONObject jResult;
// this is called whenever you call puhlishProgress(Integer), for example
// when updating a progressbar when downloading stuff
protected void onProgressUpdate(Integer... progress) {
}
// the onPostexecute method receives the return type of doInBackGround()
protected void onPostExecute(JSONObject result) {
delegate.processFinish(result);
}
@Override
protected JSONObject doInBackground(String... params) {
InputStream is = null;
JSONObject jObj = null;
String json = null;
Log.e("Params 0", params[0]);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
Here is the JSON Object I am trying to parse:
{"Bus":[{"pkey":604,"vehicle_id":261,"trip_id":945162,"route_id":2942,"stop_id":1847,"latitude":43.26762008666992,"longitude":-79.9635009765625,"speed":3.0,"label":"504","bearing":110.0,"odometer":1329300.0,"timestamp":1420775313},{"pkey":605,"vehicle_id":284,"trip_id":945024,"route_id":2942,"stop_id":1926,"latitude":43.22480010986328,"longitude":-79.7864990234375,"speed":10.0,"label":"703","bearing":296.0,"odometer":9481600.0,"timestamp":1420775308},{"pkey":607,"vehicle_id":388,"trip_id":944939,"route_id":2942,"stop_id":1368,"latitude":43.247520446777344,"longitude":-79.84809875488281,"speed":12.0,"label":"1008","bearing":288.0,"odometer":2.21116E+7,"timestamp":1420775317},{"pkey":610,"vehicle_id":422,"trip_id":945158,"route_id":2942,"stop_id":2145,"latitude":43.2575798034668,"longitude":-79.92250061035156,"speed":0.0,"label":"1205","bearing":98.0,"odometer":203000.0,"timestamp":1420775296},{"pkey":623,"vehicle_id":402,"trip_id":945023,"route_id":2942,"stop_id":1955,"latitude":43.25736999511719,"longitude":-79.87146759033203,"speed":8.0,"label":"1102","bearing":286.0,"odometer":5067800.0,"timestamp":1420775311},{"pkey":630,"vehicle_id":276,"trip_id":945154,"route_id":2942,"stop_id":3089,"latitude":43.25257110595703,"longitude":-79.85984802246094,"speed":4.0,"label":"518","bearing":184.0,"odometer":5778500.0,"timestamp":1420775286},{"pkey":634,"vehicle_id":294,"trip_id":945161,"route_id":2942,"stop_id":1923,"latitude":43.23030090332031,"longitude":-79.79962158203125,"speed":13.0,"label":"713","bearing":110.0,"odometer":1.7729E+7,"timestamp":1420775313},{"pkey":639,"vehicle_id":335,"trip_id":944997,"route_id":2942,"stop_id":1320,"latitude":43.257320404052734,"longitude":-79.93509674072266,"speed":2.0,"label":"819","bearing":272.0,"odometer":4.09802E+7,"timestamp":1420775293},{"pkey":653,"vehicle_id":425,"trip_id":944985,"route_id":2942,"stop_id":2560,"latitude":43.23651885986328,"longitude":-79.97097778320312,"speed":13.0,"label":"1208","bearing":210.0,"odometer":5824800.0,"timestamp":1420775288},{"pkey":660,"vehicle_id":317,"trip_id":945168,"route_id":2942,"stop_id":2530,"latitude":43.20967102050781,"longitude":-79.78714752197266,"speed":0.0,"label":"801","bearing":272.0,"odometer":428900.0,"timestamp":1420775282}]}
Log Output
01-09 00:05:29.053: W/System.err(21902): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:9000 refused
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:235)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1227)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:567)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:491)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:469)
01-09 00:05:29.053: W/System.err(21902): at com.example.myshuttle.ServerCommunication.doInBackground(ServerCommunication.java:46)
01-09 00:05:29.053: W/System.err(21902): at com.example.myshuttle.ServerCommunication.doInBackground(ServerCommunication.java:1)
01-09 00:05:29.053: W/System.err(21902): at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-09 00:05:29.053: W/System.err(21902): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-09 00:05:29.053: W/System.err(21902): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-09 00:05:29.053: W/System.err(21902): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-09 00:05:29.053: W/System.err(21902): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-09 00:05:29.053: W/System.err(21902): at java.lang.Thread.run(Thread.java:841)
01-09 00:05:29.053: W/System.err(21902): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 9000): connect failed: ECONNREFUSED (Connection refused)
01-09 00:05:29.053: W/System.err(21902): at libcore.io.IoBridge.connect(IoBridge.java:114)
01-09 00:05:29.053: W/System.err(21902): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-09 00:05:29.053: W/System.err(21902): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
01-09 00:05:29.053: W/System.err(21902): at java.net.Socket.connect(Socket.java:833)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-09 00:05:29.053: W/System.err(21902): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:188)
01-09 00:05:29.053: W/System.err(21902): ... 15 more
01-09 00:05:29.053: W/System.err(21902): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
01-09 00:05:29.053: W/System.err(21902): at libcore.io.Posix.connect(Native Method)
01-09 00:05:29.053: W/System.err(21902): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-09 00:05:29.053: W/System.err(21902): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-09 00:05:29.053: W/System.err(21902): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-09 00:05:29.053: W/System.err(21902): ... 20 more
Any suggestions?