我包含的代码是我的Asynctask,它从URL获取JSON并尝试解析它并将信息输出到屏幕上的TextViews。我假设问题在于onPostExecute块实际上将JSON信息传递给字符串并输出它但我无法弄清楚我做错了什么。昨晚我让它部分工作,但它只打印0和零。我已经做了一些改变,我在这里。
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)
答案 0 :(得分:1)
{ // json object node
"nv43": { // jsonobject nv43
"id": 23591778,
"name": "NV43",
"profileIconId": 22,
"summonerLevel": 30,
"revisionDate": 1396737099000
}
}
您错过了解析JSONObject
nv43。
在doInBackground
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject jb = json.getJSONObject("nv43");
return jb;
答案 1 :(得分:0)
更改下面的onPostExecute()方法
@Override
protected void onPostExecute(JSONObject json) {
try {
JSONObject result = json.getJSONObject("nv43"); // First Create an Object of "nv43"
// Storing JSON item to String
String id = result.getString(TAG_ID);
String name = result.getString(TAG_NAME);
// Rest code