我正在尝试从网页解析JSON对象。我知道你必须使用AsyncTask来创建一个运行网络操作的线程。但是,我仍然无法从网站解析字符串并创建JSON对象。当我尝试解析数据时,异常最终会被抛入我的AsyncTask类中。
例外是:java.lang.NullPointerException:lock == null
我从我的主活动类调用我的ServerCommunication(AsyncTask)类,并向我的ServerCommunication对象传递一个包含我想要解析的JSON对象的url。然后在我的ServerCommunication类中运行doInBackground()方法。 doInBackground()中的第二个try-catch语句是抛出异常的地方,我不确定原因。
此问题已被标记为重复,但我认为这是一个不同的问题。我问为什么这个java.lang.NullPointException:在我的doInBackground()方法中抛出了锁= = null。我不是在问onPostExecute()
这是我的扩展AsyncTask类:
package com.example.myshuttle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.Log;
class ServerCommunication extends AsyncTask<String, Integer, JSONObject> {
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) {
jResult = result;
}
@Override
protected JSONObject doInBackground(String... params) {
InputStream is = null;
JSONObject jObj = null;
String json = null;
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 + "n");
}
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;
}
}
这是我的主要活动类:
package com.example.myshuttle;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import com.example.myshuttle.FeedFiles.Agency;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.VisibleRegion;
public class MainActivity extends Activity {
public static ServerCommunication sc = new ServerCommunication();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sc.execute("http://localhost:9000/getVehicle/2942");
JSONObject json;
json = sc.jResult;
Log.e("JSON: ", json.toString());
}
}
这是我的堆栈跟踪:
01-07 16:17:40.015: E/Buffer Error(13328): Error converting result java.lang.NullPointerException: lock == null
这是JSON对象
{"Bus":[{"pkey":5714,"vehicle_id":267,"trip_id":944994,"route_id":2942,"stop_id":2528,"latitude":43.2169189453125,"longitude":-79.78431701660156,"speed":9.0,"label":"510","bearing":16.0,"odometer":1.652823E+8,"timestamp":1420667856},{"pkey":5716,"vehicle_id":242,"trip_id":944945,"route_id":2942,"stop_id":1323,"latitude":43.22922134399414,"longitude":-79.80352020263672,"speed":7.0,"label":"319","bearing":16.0,"odometer":826200.0,"timestamp":1420667849},{"pkey":5720,"vehicle_id":237,"trip_id":945063,"route_id":2942,"stop_id":3166,"latitude":43.239749908447266,"longitude":-79.96507263183594,"speed":18.0,"label":"314","bearing":74.0,"odometer":131516399.99999999,"timestamp":1420667854},{"pkey":5725,"vehicle_id":303,"trip_id":944995,"route_id":2942,"stop_id":1504,"latitude":43.240570068359375,"longitude":-79.83180236816406,"speed":4.0,"label":"722","bearing":16.0,"odometer":434100.0,"timestamp":1420667847},{"pkey":5733,"vehicle_id":387,"trip_id":944926,"route_id":2942,"stop_id":1799,"latitude":43.21672058105469,"longitude":-79.7544174194336,"speed":0.0,"label":"1007","bearing":100.0,"odometer":2.32553E+7,"timestamp":1420667844},{"pkey":5735,"vehicle_id":402,"trip_id":945014,"route_id":2942,"stop_id":1964,"latitude":43.25659942626953,"longitude":-79.8690185546875,"speed":2.0,"label":"1102","bearing":294.0,"odometer":4667100.0,"timestamp":1420667858},{"pkey":5737,"vehicle_id":439,"trip_id":945136,"route_id":2942,"stop_id":2158,"latitude":43.259700775146484,"longitude":-79.88922119140625,"speed":9.0,"label":"1222","bearing":106.0,"odometer":1.83482E+7,"timestamp":1420667849},{"pkey":5743,"vehicle_id":379,"trip_id":945139,"route_id":2942,"stop_id":1643,"latitude":43.2689208984375,"longitude":-79.97081756591797,"speed":5.0,"label":"917","bearing":292.0,"odometer":83700.0,"timestamp":1420667832},{"pkey":5748,"vehicle_id":364,"trip_id":944996,"route_id":2942,"stop_id":2672,"latitude":43.24897003173828,"longitude":-79.85856628417969,"speed":2.0,"label":"902","bearing":288.0,"odometer":2.19297E+7,"timestamp":1420667856},{"pkey":5765,"vehicle_id":440,"trip_id":945135,"route_id":2942,"stop_id":2671,"latitude":43.24937057495117,"longitude":-79.86029815673828,"speed":10.0,"label":"1223","bearing":108.0,"odometer":3.23947E+7,"timestamp":1420667855},{"pkey":5780,"vehicle_id":320,"trip_id":945138,"route_id":2942,"stop_id":2353,"latitude":43.26076889038086,"longitude":-79.9448471069336,"speed":6.0,"label":"804","bearing":144.0,"odometer":6.015712E+8,"timestamp":1420667848},{"pkey":5788,"vehicle_id":428,"trip_id":945015,"route_id":2942,"stop_id":2155,"latitude":43.25885009765625,"longitude":-79.90489959716797,"speed":6.0,"label":"1211","bearing":258.0,"odometer":1073400.0,"timestamp":1420667856},{"pkey":5807,"vehicle_id":287,"trip_id":945137,"route_id":2942,"stop_id":2190,"latitude":43.25831985473633,"longitude":-79.91905212402344,"speed":2.0,"label":"706","bearing":40.0,"odometer":6.99237E+7,"timestamp":1420667836},{"pkey":5817,"vehicle_id":408,"trip_id":945075,"route_id":2942,"stop_id":2673,"latitude":43.24742126464844,"longitude":-79.85266876220703,"speed":7.0,"label":"1108","bearing":108.0,"odometer":2.091137E+8,"timestamp":1420667857},{"pkey":5818,"vehicle_id":247,"trip_id":945017,"route_id":2942,"stop_id":1385,"latitude":43.263519287109375,"longitude":-79.9510498046875,"speed":10.0,"label":"404","bearing":74.0,"odometer":3.03674E+7,"timestamp":1420667855},{"pkey":5819,"vehicle_id":301,"trip_id":945074,"route_id":2942,"stop_id":1802,"latitude":43.23554992675781,"longitude":-79.82077026367188,"speed":1.0,"label":"720","bearing":106.0,"odometer":597700.0,"timestamp":1420667838},{"pkey":5859,"vehicle_id":256,"trip_id":945016,"route_id":2942,"stop_id":2189,"latitude":43.25862121582031,"longitude":-79.91905212402344,"speed":0.0,"label":"413","bearing":172.0,"odometer":4.62594E+7,"timestamp":1420667852},{"pkey":5863,"vehicle_id":288,"trip_id":944999,"route_id":2942,"stop_id":1572,"latitude":43.22637176513672,"longitude":-79.94774627685547,"speed":14.0,"label":"707","bearing":76.0,"odometer":4.90179E+7,"timestamp":1420667853},{"pkey":5876,"vehicle_id":212,"trip_id":945061,"route_id":2942,"stop_id":1953,"latitude":43.234100341796875,"longitude":-79.81622314453125,"speed":1.0,"label":"206","bearing":122.0,"odometer":1313300.0,"timestamp":1420667857}]}