【发布时间】:2017-03-12 13:23:44
【问题描述】:
我有一个 JSON 文件并将其上传到我的服务器上,然后我在我的 android 应用程序中获取它。但是每次我尝试调试我的应用程序时,它都会在 AsyncTask 类中以 Null Pointer Exception 崩溃并给我这个错误:
10-30 05:07:46.268 8710-9073/com.example.prof_mohamedatef.listview E/AndroidRuntime:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.prof_mohamedatef.listview.androidlistviewactivity$FetchUsersDesires.doInBackground(androidlistviewactivity.java:159)
at com.example.prof_mohamedatef.listview.androidlistviewactivity$FetchUsersDesires.doInBackground(androidlistviewactivity.java:84)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
我确信我的设备已连接到互联网。我采用的 url 可能构建错误,可能需要问号 ? 或任何其他 symbols,或者我的 json 文件可能没有' t 构造正确。请查看我的网址:https://yourbest-online.com/images/xml_files/Desires.json 和我的 json 代码:
{"page":1,"Desires": [{
"hotels": {
"id": "1",
"title": "hotels booking",
"thumb_url": "https://yourbest-online.com/images/xml_files/hotels.png"
},
"Aviation": {
"id": "2",
"title": "Aviation Tickets Booking",
"thumb_url": "https://yourbest-online.com/images/xml_files/aviation.png"
}
],"total_results":2,"total_pages":1}
这是我所有的 AsyncTask 类,包括问题总是发生的 doInBackground 方法:
public class FetchUsersDesires extends AsyncTask<String, Void, ArrayList<OptionsEntity>> {
private final String LOG_TAG = FetchUsersDesires.class.getSimpleName();
private ArrayList<OptionsEntity> getUsersDesiresFromJson(String UsersDesires)
throws JSONException {
UsersDesiresJson = new JSONObject(UsersDesires);
UsersDesiresJsonAray = UsersDesiresJson.getJSONArray(main_List);
list.clear();
for (int i = 0; i < UsersDesiresJsonAray.length(); i++) {
// Get the JSON object representing a movie per each loop
oneOptionData = UsersDesiresJsonAray.getJSONObject(i);
ID_STRING = oneOptionData .getString(ID);
TITLE_STRING = oneOptionData .getString(TITLE);
Image_URL_STRING = oneOptionData .getString(Image_URL);
mAdapter=null;
OptionsEntity entity = new OptionsEntity(Image_URL_STRING, TITLE_STRING);
list.add(entity);
}
return list;
}
@Override
protected ArrayList<OptionsEntity> doInBackground(String... params) {
String UsersDesires_JsonSTR = null;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
if (params.length == 0) {
return null;
}
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
UsersDesires_JsonSTR = null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
UsersDesires_JsonSTR = buffer.toString();
Log.v(LOG_TAG, "Users Desires String: " + UsersDesires_JsonSTR);
} catch (IOException e) {
Log.e(LOG_TAG, "Error here Exactly ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
return getUsersDesiresFromJson(UsersDesires_JsonSTR);
} catch (JSONException e) {
Log.e(LOG_TAG, "didn't got Users Desires from getJsonData method", e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<OptionsEntity> result) {
if (result != null&& getApplicationContext()!=null) {
mAdapter = new LazyAdapter(getApplicationContext(),R.layout.list_row, result);
listView.setAdapter(mAdapter);
}
}
}
注意,连接现在以最佳方式运行,但 JSON Exception 出现:“No Value for id”和 json android studio 中的字符串变量值如下所示:"{"page":1,"Desires": [{ “酒店”:{ “id”:“1”, "title": "酒店预订", "thumb_url": "https://yourbest-online.com/images/xml_files/hotels.png" }, “航空”:{ “id”:“2”, "title": "机票预订", "thumb_url": "https://yourbest-online.com/images/xml_files/aviation.png" } }],"total_results":2,"total_pages":1} " 该值似乎为每个空格包含多个 \n\t\t\t 符号,我认为这是我造成的问题,请为此提供任何建议。
您的帮助将不胜感激。谢谢你
【问题讨论】:
-
您是否因为没有从 url 获取任何数据而收到空指针异常?或者原因不一样!!
-
空指针异常说明了什么?
-
@ginomempin 审查编辑空指针异常
-
而且,不,你的 JSON is not valid
-
您没有编辑您的 JSON.. 是吗?它仍然无效。这里有一个建议:使用带有 Gson 转换器或 OkHttp 或 Volley 的 Retrofit 来执行您的网络请求。在我看来,没有太多理由使用 HttpURLConnection。特别是如果您有大量 JSON 文件要处理
标签: android json listview android-asynctask nullpointerexception