gpt4 book ai didi

android - 检索 Json 数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:58 25 4
gpt4 key购买 nike

我正在尝试从以下网址检索值:http://rentopoly.com/ajax.php?query=Bo .我想将所有建议的值一一显示在 ListView 中。这就是我想要做的...

public class AlertsAdd {
public ArrayList <JSONObject> retrieveJSONArray(String urlString) {
String result = queryRESTurl(urlString);
ArrayList <JSONObject> ALERTS = new ArrayList <JSONObject> ();
if (result != null) {

try {
JSONObject json = new JSONObject(result);
JSONArray alertsArray = json.getJSONArray("suggestions");
for (int a = 0; a < alertsArray.length(); a++) {
JSONObject alertitem = alertsArray.getJSONObject(a);
ALERTS.add(alertitem);
}
return ALERTS;
} catch (JSONException e) {
Log.e("JSON", "There was an error parsing the JSON", e);
}
}
JSONObject myObject = new JSONObject();
try {
myObject.put("suggestions", myObject.getJSONArray("suggestions"));
ALERTS.add(myObject);
} catch (JSONException e1) {
Log.e("JSON", "There was an error creating the JSONObject", e1);
}
return ALERTS;
}
private String queryRESTurl(String url) {
// URLConnection connection;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
instream.close();
return result;
}
} catch (ClientProtocolException e) {
Log.e("REST", "There was a protocol based error", e);
} catch (IOException e) {
Log.e("REST", "There was an IO Stream related error", e);
}
return null;
}

/**
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

这是适配器代码...

public class AlertsAdapter extends ArrayAdapter <JSONObject> {
public AlertsAdapter(Activity activity, List <JSONObject> alerts) {
super(activity, 0, alerts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_text, null);
JSONObject imageAndText = getItem(position);
TextView textView = (TextView) rowView.findViewById(R.id.last_build_stat);
try {
textView.setText((String) imageAndText.get("suggestions"));
} catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}

这是 logcat...

    04-30 13:09:46.656: INFO/ActivityManager(584): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.WorldToyota/.Alerts }
04-30 13:09:50.417: ERROR/JSON(924): There was an error parsing the JSON
04-30 13:09:50.417: ERROR/JSON(924): org.json.JSONException: JSONArray[0] is not a JSONObject.
04-30 13:09:50.417: ERROR/JSON(924): at org.json.JSONArray.getJSONObject(JSONArray.java:268)
04-30 13:09:50.417: ERROR/JSON(924): at com.WorldToyota.AlertsAdd.retrieveJSONArray(AlertsAdd.java:30)
04-30 13:09:50.417: ERROR/JSON(924): at com.WorldToyota.Alerts.onCreate(Alerts.java:20)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
04-30 13:09:50.417: ERROR/JSON(924): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 13:09:50.417: ERROR/JSON(924): at android.os.Looper.loop(Looper.java:123)
04-30 13:09:50.417: ERROR/JSON(924): at android.app.ActivityThread.main(ActivityThread.java:4203)
04-30 13:09:50.417: ERROR/JSON(924): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:09:50.417: ERROR/JSON(924): at java.lang.reflect.Method.invoke(Method.java:521)
04-30 13:09:50.417: ERROR/JSON(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-30 13:09:50.417: ERROR/JSON(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-30 13:09:50.417: ERROR/JSON(924): at dalvik.system.NativeStart.main(Native Method)
04-30 13:09:50.688: ERROR/JSON(924): There was an error creating the JSONObject
04-30 13:09:50.688: ERROR/JSON(924): org.json.JSONException: JSONObject["suggestions"] not found.
04-30 13:09:50.688: ERROR/JSON(924): at org.json.JSONObject.get(JSONObject.java:287)
04-30 13:09:50.688: ERROR/JSON(924): at org.json.JSONObject.getJSONArray(JSONObject.java:362)
04-30 13:09:50.688: ERROR/JSON(924): at com.WorldToyota.AlertsAdd.retrieveJSONArray(AlertsAdd.java:41)
04-30 13:09:50.688: ERROR/JSON(924): at com.WorldToyota.Alerts.onCreate(Alerts.java:20)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
04-30 13:09:50.688: ERROR/JSON(924): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 13:09:50.688: ERROR/JSON(924): at android.os.Looper.loop(Looper.java:123)
04-30 13:09:50.688: ERROR/JSON(924): at android.app.ActivityThread.main(ActivityThread.java:4203)
04-30 13:09:50.688: ERROR/JSON(924): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:09:50.688: ERROR/JSON(924): at java.lang.reflect.Method.invoke(Method.java:521)
04-30 13:09:50.688: ERROR/JSON(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-30 13:09:50.688: ERROR/JSON(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-30 13:09:50.688: ERROR/JSON(924): at dalvik.system.NativeStart.main(Native Method)

请帮我解析这个脚本并以列表格式显示值....

最佳答案

查看此示例 - 使用您尝试访问的相同服务并且也是基于 Android。

http://damonsk.com/2010/01/jsonarray-httpclient-android/

关于android - 检索 Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2743066/

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