gpt4 book ai didi

java - Android Studio - 从 URL 获取 JSON 致命异常

转载 作者:行者123 更新时间:2023-12-01 19:31:26 26 4
gpt4 key购买 nike

所以我在让这段代码工作时遇到了一些问题,我在网上找到了一些这段代码,但它不起作用。我尝试过调整一些代码,但无论我在做什么,应用程序都会因错误而崩溃。几天来,我一直在尝试让它发挥作用,用了几个小时的谷歌搜索和观看 YouTube 教程,但我似乎无法让它发挥作用。

有人可以阐明我在这里做错了什么吗?

代码:

public class fetchJSON extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {

HttpURLConnection urlConnection = null;
BufferedReader reader = null;

URL url;
try {
url = new URL("www.link.to.json");
urlConnection.setRequestMethod("GET"); //Your method here
urlConnection.connect();

InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null)
buffer.append(line + "\n");

if (buffer.length() == 0)
return null;

return buffer.toString();
} catch (IOException e) {
Log.e(TAG, "IO Exception", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(TAG, "Error closing stream", e);
}
}
}
}

@Override
protected void onPostExecute(String response) {
if(response != null) {
JSONObject json = null;
try {
json = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonResponse = null;
try {
jsonResponse = json.getJSONObject("Question1");
} catch (JSONException e) {
e.printStackTrace();
}
MainActivity.jsonPrint.setText((CharSequence) jsonResponse);
}
}
}

错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: uk.co.jrtevents.fetchjsonfromurltest, PID: 4019
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
at uk.co.jrtevents.fetchjsonfromurltest.fetchJSON.doInBackground(fetchJSON.java:31)
at uk.co.jrtevents.fetchjsonfromurltest.fetchJSON.doInBackground(fetchJSON.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

第 31 行:urlConnection.setRequestMethod("GET");

最佳答案

HttpURLConnection urlConnection = null;

您从未给 urlConnection 赋值,因此它是 null

您需要添加:

urlConnection = url.openConnection();

在尝试使用urlConnection之前。

URLHttpURLConnection 非常古老,很少有开发人员再使用它们。使用更现代的 HTTP 客户端 API,您可能会有更好的运气,例如 OkHttp .

关于java - Android Studio - 从 URL 获取 JSON 致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699004/

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