我正在尝试在我的Android应用程序中解析JSON格式。我是开发Android应用程序的新手,所以我指的是很多教程。我按照本教程中的步骤进行操作:http://mobilesiri.com/json-parsing-in-android-using-android-studio/
这是我的MainActivity.java类:
package com.example.carlajoyce.parsejson;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends ListActivity {
private static String url = "http://tech.ccare.jrhernani.com/tech/workorders";
private static final String TAG_WORKORDERINFO = "workorders";
private static final String TAG_ID = "id";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_INITIATED = "initiated";
private static final String TAG_FINISHED = "finished";
private static final String TAG_CREATEDAT = "created_at";
private static final String TAG_UPDATEDAT = "updated_at";
private static final String TAG_TICKETID = "ticket_id";
private static final String TAG_CREATEDBYID = "createdBy_id";
private static final String TAG_TECHNICIANID = "technician_id";
private static final String TAG_WORKORDERREFID = "workorderref_id";
private static final String TAG_WORKORDERSTATUSID = "workorderstatus_id";
private static final String TAG_DEPARTMENTID = "department_id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetWorkOrders().execute();
}
/**
*Async task class to get JSON by making HTTP call
*/
private class GetWorkOrders extends AsyncTask<Void, Void, Void> {
/*HashMap for ListView*/
ArrayList<HashMap<String, String>> workOrderList;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
/*Creating service handler class instance*/
WebRequest webreq = new WebRequest();
/* Making a request to url and getting response*/
String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GETRequest);
Log.d("Response: ", "> " + jsonStr);
workOrderList = ParseJSON(jsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
/*Dismiss the progress dialog*/
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, workOrderList,
R.layout.list_item, new String[]{TAG_SUBJECT, TAG_DESCRIPTION}, new int[]{R.id.subject, R.id.description});
setListAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseJSON(String json) {
if (json != null) {
try {
// Hashmap for ListView
ArrayList<HashMap<String, String>> workOrderList = new ArrayList<HashMap<String, String>>();
JSONObject jsonObj = new JSONObject(json);
// Getting JSON Array node
JSONArray workOrders = jsonObj.getJSONArray(TAG_WORKORDERINFO);
// looping through All WorkOrders
for (int i = 0; i < workOrders.length(); i++) {
JSONObject c = workOrders.getJSONObject(i);
String id = c.getString(TAG_ID);
String subject = c.getString(TAG_SUBJECT);
String description = c.getString(TAG_DESCRIPTION);
String initiated = c.getString(TAG_INITIATED);
String finished = c.getString(TAG_FINISHED);
String created_at = c.getString(TAG_CREATEDAT);
String updated_at = c.getString(TAG_UPDATEDAT);
String ticket_id = c.getString(TAG_TICKETID);
String createdBy_id = c.getString(TAG_CREATEDBYID);
String technician_id = c.getString(TAG_TECHNICIANID);
String workorderref_id = c.getString(TAG_WORKORDERREFID);
String workorderstatus_id = c.getString(TAG_WORKORDERSTATUSID);
String department_id = c.getString(TAG_DEPARTMENTID);
// tmp hashmap for single student
HashMap<String, String> workOrder = new HashMap<String, String>();
// adding each child node to HashMap key => value
workOrder.put(TAG_ID,id);
workOrder.put(TAG_SUBJECT, subject);
workOrder.put(TAG_DESCRIPTION, description);
workOrder.put(TAG_INITIATED, initiated);
workOrder.put(TAG_FINISHED, finished);
workOrder.put(TAG_CREATEDAT, created_at);
workOrder.put(TAG_UPDATEDAT, updated_at);
workOrder.put(TAG_TICKETID, ticket_id);
workOrder.put(TAG_CREATEDBYID, createdBy_id);
workOrder.put(TAG_TECHNICIANID, technician_id);
workOrder.put(TAG_WORKORDERREFID, workorderref_id);
workOrder.put(TAG_WORKORDERSTATUSID, workorderstatus_id);
workOrder.put(TAG_DEPARTMENTID, department_id);
// adding student to students list
workOrderList.add(workOrder);
}
return workOrderList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url.");
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我对代码如何工作有一个基本的了解,我修复了一些问题,而不必在这里问,但我遇到了一个问题,我不知道从哪里开始。我希望你能告诉我这些错误的原因:
02-16 17:51:31.294 6378-6378/com.example.carlajoyce.parsejson E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.carlajoyce.parsejson, PID: 6378
java.lang.NullPointerException
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.ListView.setAdapter(ListView.java:480)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at com.example.carlajoyce.parsejson.MainActivity$GetWorkOrders.onPostExecute(MainActivity.java:93)
at com.example.carlajoyce.parsejson.MainActivity$GetWorkOrders.onPostExecute(MainActivity.java:49)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
at dalvik.system.NativeStart.main(Native Method)
注意:我还下载了教程提供的代码,它运行正常。 提前谢谢。
答案 0 :(得分:0)
我认为你应该先用空列表初始化适配器和视图,所以移动它:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, workOrderList,
R.layout.list_item, new String[]{TAG_SUBJECT, TAG_DESCRIPTION}, new int[]{R.id.subject, R.id.description});
setListAdapter(adapter);
到MainActivity中的onCreate(),当作业完成时 - 在onPostExecute()中只通知适配器数据集已更改。
adapter.notifyDataSetChanged();
如果它适合您,请告诉我。
答案 1 :(得分:-1)
如果workOrderList
为空,则会发生这种情况。
在调用适配器的构造函数之前,您必须初始化此列表。
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, workOrderList,
R.layout.list_item, new String[]{TAG_SUBJECT, TAG_DESCRIPTION}, new int[]{R.id.subject, R.id.description});
这是因为您的ParseJSON
(在doInBackground
方法中调用)可以返回null