gpt4 book ai didi

java - 如何解析这个简单的 JSON 数据

转载 作者:行者123 更新时间:2023-12-02 06:10:39 26 4
gpt4 key购买 nike

我一直在学习在Android中解析json数据。我能够解析我所关注的示例中的 json 数据。为了测试我自己,我开始解析一些新的 json 数据。我发现了下面这个 json 数据:

    {"errors":[{"message":"Bad Authentication data","code":215}]}

我无法解析它:(

下面是我解析 json 的代码:

    public JSONObject getJSONFromURL(String url){
JSONObject receivedJSON = null;

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200){
return null;
}
//Getting stream of data returned from the server using InputStream
InputStream streamReceived = response.getEntity().getContent();
//Reading stream of data using BufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(streamReceived));
StringBuilder builder = new StringBuilder();
String line = null;
//Reading each line of the received data
while((line = reader.readLine()) != null){
builder.append(line);
}
streamReceived.close();
//Converting the data read into String using StringBuilder
String jsonData = builder.toString();
//Converting String into JSONObject
receivedJSONData = new JSONObject(jsonData);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//returning the JSONObject
return receivedJSON;
}

在我的 MainActivity 中,我获取 JSONObject 并相应地从中获取信息。这是我的做法:

    JSONObject jsonObj = jsonParser.getJSONFromURL(twitterURL);
JSONArray erros = jsonObj.getJSONArray("errors");
JSONObject error = erros.getJSONObject(0);
String msg = error.getString("message");
Log.d("Twitter Data", msg);

我很困惑是否将上述 json 字符串中的“错误”视为对象或数组。

下面是我的 logcat 错误:

02-21 10:21:08.391: E/AndroidRuntime(972): FATAL EXCEPTION: AsyncTask #1
02-21 10:21:08.391: E/AndroidRuntime(972): java.lang.RuntimeException: An error occured while executing doInBackground()
02-21 10:21:08.391: E/AndroidRuntime(972): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-21 10:21:08.391: E/AndroidRuntime(972): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.lang.Thread.run(Thread.java:856)
02-21 10:21:08.391: E/AndroidRuntime(972): Caused by: java.lang.NullPointerException
02-21 10:21:08.391: E/AndroidRuntime(972): at com.example.youtubevideolist.MainActivity$FetchYoutubeVideos.doInBackground(MainActivity.java:77)
02-21 10:21:08.391: E/AndroidRuntime(972): at com.example.youtubevideolist.MainActivity$FetchYoutubeVideos.doInBackground(MainActivity.java:1)
02-21 10:21:08.391: E/AndroidRuntime(972): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 10:21:08.391: E/AndroidRuntime(972): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 10:21:08.391: E/AndroidRuntime(972): ... 4 more
02-21 10:21:09.731: E/WindowManager(972): Activity com.example.youtubevideolist.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40ce51d8 V.E..... R......D 0,0-729,324} that was originally added here
02-21 10:21:09.731: E/WindowManager(972): android.view.WindowLeaked: Activity com.example.youtubevideolist.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40ce51d8 V.E..... R......D 0,0-729,324} that was originally added here
02-21 10:21:09.731: E/WindowManager(972): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
02-21 10:21:09.731: E/WindowManager(972): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
02-21 10:21:09.731: E/WindowManager(972): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-21 10:21:09.731: E/WindowManager(972): at android.app.Dialog.show(Dialog.java:281)
02-21 10:21:09.731: E/WindowManager(972): at com.example.youtubevideolist.MainActivity$FetchYoutubeVideos.onPreExecute(MainActivity.java:56)
02-21 10:21:09.731: E/WindowManager(972): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-21 10:21:09.731: E/WindowManager(972): at android.os.AsyncTask.execute(AsyncTask.java:534)
02-21 10:21:09.731: E/WindowManager(972): at com.example.youtubevideolist.MainActivity.onCreate(MainActivity.java:38)
02-21 10:21:09.731: E/WindowManager(972): at android.app.Activity.performCreate(Activity.java:5104)
02-21 10:21:09.731: E/WindowManager(972): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-21 10:21:09.731: E/WindowManager(972): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-21 10:21:09.731: E/WindowManager(972): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-21 10:21:09.731: E/WindowManager(972): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-21 10:21:09.731: E/WindowManager(972): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-21 10:21:09.731: E/WindowManager(972): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 10:21:09.731: E/WindowManager(972): at android.os.Looper.loop(Looper.java:137)
02-21 10:21:09.731: E/WindowManager(972): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 10:21:09.731: E/WindowManager(972): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 10:21:09.731: E/WindowManager(972): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 10:21:09.731: E/WindowManager(972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 10:21:09.731: E/WindowManager(972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 10:21:09.731: E/WindowManager(972): at dalvik.system.NativeStart.main(Native Method)

非常感谢对此的任何帮助:) 提前致谢:)

最佳答案

信息部分是:

Caused by: java.lang.NullPointerException
at com.example.youtubevideolist.MainActivity$FetchYoutubeVideos.doInBackground(MainActivity.java:77)

所以该行有一个 NullPointerException。

如果您还没有看到问题,欢迎您在发布的代码中标记该行,这里的人员会告诉您可能会出现问题的地方。

关于java - 如何解析这个简单的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com