我试图将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);
}
});
}
}
答案 0 :(得分:1)
您没有显示正在解析表单的确切URL,但问题不在于解析器需要授权(需要登录)才能从该页面获取真正的JSON。
您可能会在该网址上找到401 Authorization Required
页面。然后jsonParser
尝试解析字符串&#34; 401 Authorization Required&#34;字符串,它是无效的JSON。您可以通过打开私人浏览器窗口并直接访问网址来验证这一点。