我是 JSON 和解析数据等方面的新手。我使用 GSON 并尝试提取 JSONArray“系统”,然后显示 JSONObject 的“system_name”,然后显示它。下面是要提取的 JSON 数据的示例。
{
"systems": [
{
"city": "Petaluma",
"country": "US",
"postal_code": "94954",
"state": "CA",
"status": "normal",
"system_id": 66,
"system_name": "Smith Residence",
"system_public_name": "Residential System",
"timezone": "America/Los_Angeles"
},
{
"city": "Atherton",
"country": "US",
"postal_code": "94954",
"state": "CA",
"status": "error",
"system_id": 77,
"system_name": "Jones Residence",
"system_public_name": "Jones Residence",
"timezone": "America/Los_Angeles"
}
]
}
API 使用 API_KEY 进行身份验证,并像这样使用
https://api.company.com/api/systems?key=123ABC
代码
public class PullJSONData extends Activity {
TextView systemsTextView;
ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
systemsTextView = (TextView) findViewById(R.id.textView);
this.retrieveSystems();
}
void retrieveSystems() {
progressDialog = ProgressDialog.show(this, "Please wait...",
"Retrieving data...", true, true);
SystemsRetrieverAsyncTask task = new SystemsRetrieverAsyncTask();
task.execute();
progressDialog.setOnCancelListener(new CancelListener(task));
}
private class SystemsRetrieverAsyncTask extends AsyncTask<Void, Void, Void> {
Response response;
@Override
protected Void doInBackground(Void... params) {
String url = "https://api.company.com/api";
HttpGet getRequest = new HttpGet(url);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse getResponse = httpClient.execute(getRequest);
final int statusCode = getResponse.getStatusLine()
.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode
+ " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
InputStream httpResponseStream = getResponseEntity.getContent();
Reader inputStreamReader = new InputStreamReader(
httpResponseStream);
Gson gson = new Gson();
SystemList systemList = gson.fromJson(jsonString, SystemList.class);
for (int i = 0; i < systemList.systems.size(); i++)
{
System s = systemList.systems.get(i);
}
this.response = gson
.fromJson(inputStreamReader, Response.class);
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
StringBuilder builder = new StringBuilder();
for (System systems : this.response.data) {
builder.append(String.format(
"<br>System: <b>%s</b><br>City: <b>%s</b><br><br>",
systems.getCountry(), systems.getCity()));
}
systemsTextView.setText(Html.fromHtml(builder.toString()));
progressDialog.cancel();
}
}
private class CancelListener implements OnCancelListener {
AsyncTask<?, ?, ?> cancellableTask;
public CancelListener(AsyncTask<?, ?, ?> task) {
cancellableTask = task;
}
@Override
public void onCancel(DialogInterface dialog) {
cancellableTask.cancel(true);
}
}
回应
public class Response {
ArrayList<System> data;
public Response() {
data = new ArrayList<System>();
}
系统
public class System {
String city;
String country;
public System() {
this.city = "";
this.country = "";
}
public String getCity() {
return this.city;
}
public String getCountry() {
return this.country;
}
系统列表
public class SystemList {
public List<System> systems;
public SystemList() { systems = new ArrayList<System>(); }
}
如果您需要查看完整源代码,请在此处查找
目前的问题
- 空指针异常
来自 URL https://api.company.com/api/systems/的错误 401
04-29 15:43:25.735:E/AndroidRuntime(3678):致命异常:主要 04-29 15:43:25.735:E/AndroidRuntime(3678):java.lang.NullPointerException 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.jaisonbrooks.enlightenme.PullJSONData$SystemsRetrieverAsyncTask.onPostExecute(PullJSONData.java:96) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.jaisonbrooks.enlightenme.PullJSONData $SystemsRetrieverAsyncTask.onPostExecute(PullJSONData.java:1) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.os.AsyncTask.finish(AsyncTask.java:631) 04-29 15:43:25.735 : E/AndroidRuntime(3678): 在 android.os.AsyncTask.access$600(AsyncTask.java:177) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.os.AsyncTask$InternalHandler.handleMessage (AsyncTask.java:644) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.os.Handler。dispatchMessage(Handler.java:99) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.os.Looper.loop(Looper.java:137) 04-29 15:43:25.735: E/ AndroidRuntime(3678): 在 android.app.ActivityThread.main(ActivityThread.java:4898) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 java.lang.reflect.Method.invoke(Method.java:511) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-29 15:43:25.735: E/AndroidRuntime(3678): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main(Native Method)Looper.loop(Looper.java:137) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.app.ActivityThread.main(ActivityThread.java:4898) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invoke(Method. java:511) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)Looper.loop(Looper.java:137) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 android.app.ActivityThread.main(ActivityThread.java:4898) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invoke(Method. java:511) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)E/AndroidRuntime(3678): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invoke(Method. java:511) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)E/AndroidRuntime(3678): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 15:43:25.735: E/AndroidRuntime(3678): at java.lang.reflect.Method.invoke(Method. java:511) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)E/AndroidRuntime(3678): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-29 15:43:25.735: E/AndroidRuntime(3678): 在 dalvik.system.NativeStart.main (本机方法)