我包含的代碼是我的AsyncTask獲取來自URL的JSON並試圖解析它並將信息輸出到屏幕上的TextViews。我假設問題在於實際將JSON信息傳遞給字符串並輸出它的onPostExecute塊,但我無法弄清楚我做錯了什麼。我昨晚部分工作,但只打印0和空。我做了一些改變,現在我在這裏。JSON對象不打印到屏幕
class GetStats extends AsyncTask<String, String, JSONObject> {
private String api_key="d96236d2-6ee3-4cfd-afa7-f41bdbc11128";
String region = MainActivity.region.toLowerCase();
String name = MainActivity.name;
String url = null;
String encodedName = null;
String encodedKey = null;
String encodedRegion = null;
// JSON Node Names
String TAG_ID = "id";
String TAG_NAME = "name";
String TAG_PROFILEICONID = "profileIconId";
String TAG_REVISIONDATE = "revisionDate";
String TAG_SUMMONERLEVEL = "summonerLevel";
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
textId = (TextView) findViewById(R.id.id);
textName = (TextView) findViewById(R.id.name);
textProfileIconId = (TextView) findViewById(R.id.profileIconId);
textRevisionDate = (TextView) findViewById(R.id.revisionDate);
textSummonerLevel = (TextView) findViewById(R.id.summonerLevel);
encodedName = URLEncoder.encode(name, "UTF-8");
encodedKey = URLEncoder.encode(api_key, "UTF-8");
encodedRegion = URLEncoder.encode(region, "UTF-8");
url = "https://prod.api.pvp.net/api/lol/" + encodedRegion + "/v1.4/summoner/by-name/" + encodedName + "?api_key=" + encodedKey;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
protected JSONObject doInBackground(String... arg0) {
JSONParser jParser = new JSONParser();
// Get JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject jb = null;
try {
jb = json.getJSONObject(encodedName);
} catch (JSONException e) {
e.printStackTrace();
}
return jb;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
// Storing JSON item to String
String id = json.getString(TAG_ID);
String name = json.getString(TAG_NAME);
String icon = json.getString(TAG_PROFILEICONID);
String revDate = json.getString(TAG_REVISIONDATE);
String sumLevel = json.getString(TAG_SUMMONERLEVEL);
// Putting JSON data in TextViews
textId.setText(id);
textName.setText(name);
textProfileIconId.setText(icon);
textRevisionDate.setText(revDate);
textSummonerLevel.setText(sumLevel);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON預期結果例如:
{"nv43":{"id":23591778,"name":"NV43","profileIconId":22,"summonerLevel":30,"revisionDate":1396737099000}}
錯誤:
04-07 13:32:18.359 22875-22875/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: edu.appdesign.leaguestats, PID: 22875
java.lang.NullPointerException
at edu.appdesign.leaguestats.StatsActivity$GetStats.onPostExecute(StatsActivity.java:110)
at edu.appdesign.leaguestats.StatsActivity$GetStats.onPostExecute(StatsActivity.java:53)
at android.os.AsyncTask.finish(AsyncTask.java)
at android.os.AsyncTask.access$600(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
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)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
製作在這裏一個NullPointerException該變化結果onPostExecute()方法:字符串ID = json.getString(TAG_ID); – Nate
@Nate發佈堆棧跟蹤。 – Raghunandan
@從服務器註銷你的json可以爲空。你確定它不爲空 – Raghunandan