我正在尝试进行一些简单的 JSON 解析以从 URL 获取数据,并更改屏幕上的一些值。但是,当前,当单击按钮时应用程序会锁定并最终崩溃。我正在从中提取数据
http://dev4.vcsonline.com/deding/vpmi_mobile/WebAPI/WebAPI/api/address
onclick 监听器和异步任务:
Pull_Data = (Button) findViewById(R.id.Pull_Data);
Pull_Data.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new MyAsyncTask().execute(URL);
}
});
private class MyAsyncTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... args) {
SetValues(args[0]);
return null;
}
JSON 拉取/操作代码:
public static JSONObject getJSONfromURL(String url){
//initialize
InputStream is = null;
String result = "";
JSONObject JArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
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();
result = sb.toString();
//client_Address1.setText(result);
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try{
JArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
return JArray;
}
public void SetValues(String url){
JSONObject json = getJSONfromURL(url);
// get the element that holds the addresses
try{
JSONArray addresses = json.getJSONArray("Address");
// get the proper items
JSONObject e = addresses.getJSONObject(0);
client_Address1.setText(e.getString("addressLineOne"));
client_Address2.setText(e.getString("addressLineTwo"));
client_Address3.setText(e.getString("addressCity") + ", " + e.getString("addressState") + " " + e.getString("addressZip"));
client_Id.setText(e.getString("clientID"));
}catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
最后,logcat 条目:
08-21 11:49:42.860: E/log_tag(30551): Error converting result java.lang.NullPointerException
08-21 11:49:42.860: E/log_tag(30551): Error parsing data org.json.JSONException: End of input at character 0 of
08-21 11:49:42.865: W/dalvikvm(30551): threadid=12: thread exiting with uncaught exception (group=0x40c101f8)
08-21 11:49:42.870: E/AndroidRuntime(30551): FATAL EXCEPTION: AsyncTask #1
08-21 11:49:42.870: E/AndroidRuntime(30551): java.lang.RuntimeException: An error occured while executing doInBackground()
08-21 11:49:42.870: E/AndroidRuntime(30551): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-21 11:49:42.870: E/AndroidRuntime(30551): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.lang.Thread.run(Thread.java:856)
08-21 11:49:42.870: E/AndroidRuntime(30551): Caused by: java.lang.NullPointerException
08-21 11:49:42.870: E/AndroidRuntime(30551): at com.example.networking.MainActivity.SetValues(MainActivity.java:353)
08-21 11:49:42.870: E/AndroidRuntime(30551): at com.example.networking.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:222)
08-21 11:49:42.870: E/AndroidRuntime(30551): at com.example.networking.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
08-21 11:49:42.870: E/AndroidRuntime(30551): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-21 11:49:42.870: E/AndroidRuntime(30551): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-21 11:49:42.870: E/AndroidRuntime(30551): ... 5 more