【发布时间】:2015-10-16 07:32:18
【问题描述】:
我正在尝试将 JSON 文件中的数据显示到 Listview 中,但出现以下解析错误。
这是我的错误:
执行doInBackground()时出错
07-25 16:01:00.754: E/JSON Parser(775): 解析数据时出错 org.json.JSONException: java.lang.Integer 类型的值 401 无法转换为 JSONObject
这是我的 JSON 文件:
{
"people": [
{
"place": "1",
"person": "Lisa",
"mark": "10"
},
{
"place": "2",
"person": "Danny",
"mark": "9"
},
{
"place": "a3",
"person": "Marc",
"thur": "13",
"mark": "8",
"total": "7"
},
{
"place": "b3",
" person": "Paul ",
"mark": "8"
},
{
"place": "c5",
"person": "Eddie",
"thur": "8",
"mark": "7",
"total": "5"
}
],
"game": "First Game",
"date": "Jul 30 2015"
}
这是我的代码:
public class InboxActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> inboxList;
// products JSONArray
JSONArray inbox = null;
// Inbox JSON url
private static final String INBOX_URL = "https://www.url.com/api/v2/projects/xxx/last_ready_run/data?api_key=xxx";
// ALL JSON node names
private static final String TAG_MESSAGES = "people";
private static final String TAG_ID = "place";
private static final String TAG_FROM = "person";
private static final String TAG_EMAIL = "thur";
private static final String TAG_SUBJECT = "mark";
private static final String TAG_DATE = "total";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_list);
// Hashmap for ListView
inboxList = new ArrayList<HashMap<String, String>>();
// Loading INBOX in Background Thread
new LoadInbox().execute();
}
/**
* Background Async Task to Load all INBOX messages by making HTTP Request
* */
class LoadInbox extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(InboxActivity.this);
pDialog.setMessage("Loading Inbox ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Inbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Inbox JSON: ", json.toString());
try {
inbox = json.getJSONArray(TAG_MESSAGES);
inbox.toString();
// looping through All messages
for (int i = 0; i < inbox.length(); i++) {
JSONObject c = inbox.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String from = c.getString(TAG_FROM);
String subject = c.getString(TAG_SUBJECT);
String date = c.getString(TAG_DATE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_FROM, from);
map.put(TAG_SUBJECT, subject);
map.put(TAG_DATE, date);
// adding HashList to ArrayList
inboxList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
InboxActivity.this, inboxList,
R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
new int[] { R.id.from, R.id.subject, R.id.date });
// updating listview
setListAdapter(adapter);
}
});
}
}
【问题讨论】:
-
您发布了很多代码,但我怀疑其中大部分都无关紧要。值 401 也不会出现在您发布的 JSON 中。如果您可以发布一个简短但完整的程序来演示该问题,那将更容易帮助...
-
谢谢乔恩,我会尽量解释清楚这个问题,即使我使用不同的 JSON,我总是会得到 401,这很令人困惑。
-
究竟是哪里抛出的异常? (同样,你有很多帖子 - 如果你能展示很多 less 代码,只是相关代码,它会更容易帮助你。)
-
我怀疑 401 实际上是 HTTP 未授权状态码,顺便说一句...
-
在 doInBackground() 中执行时发生错误,我觉得我需要包含更多代码来更清楚地解释我想要实现的目标,如果太多了,抱歉。
标签: java android arrays json android-listview