gpt4 book ai didi

java - 解析 JSON - JSONException : Integer cannot be converted to JSONObject

转载 作者:行者123 更新时间:2023-12-01 11:17:34 25 4
gpt4 key购买 nike

我尝试将 JSON 文件中的数据显示到 ListView 中,但出现以下解析错误。

这是我的错误:

执行 doInBackground() 时发生错误

07-25 16:01:00.754: E/JSON 解析器(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);
}
});

}

}

最佳答案

您没有显示正在解析表单的确切网址,但问题不在于解析器,而是您需要拥有授权(需要登录)才能从该页面获取真正的 JSON。

您可能会在该网址收到 401 Authorization Needed 页面。然后,jsonParser 尝试解析字符串“401 Authorization required”,该字符串不是有效的 JSON。您可以通过打开私有(private)浏览器窗口并直接访问该网址来验证这一点。

关于java - 解析 JSON - JSONException : Integer cannot be converted to JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31630653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com