gpt4 book ai didi

java.lang.String 无法转换为 JSONArray

转载 作者:行者123 更新时间:2023-11-29 08:55:44 24 4
gpt4 key购买 nike

当我运行这个程序时,我得到了这个错误。我不知道如何解决。请帮我找到它。

12-02 23:04:34.427: E/JSON Parser(1629): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

代码:

public class Http
{

public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
private static HttpClient getHttpClient() {

if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();

final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}

return mHttpClient;
}


public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
try {


HttpClient client = getHttpClient();

HttpGet request = new HttpGet();

request.setURI(new URI(url));

HttpResponse response = client.execute(request);

try {
// Get our response as a String.
String jsonString = EntityUtils.toString(response.getEntity());



// Parse the JSON String into a JSONArray object.
return JSONArray(jsonString);

} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;

}



public static JSONArray retrieveJSON(){
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build());

String getAllFreebiesURL="http://10.0.2.2/football365/cityList.php";
JSONArray json = null;
try {
json = getJSONArrayFromUrl(getAllFreebiesURL);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("JSON",json+"A");
//JSONArray json1 = new JSONArray(json);
//json1.put(json);
/*try {
System.out.println(json1.get(2));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return json;
}
}
}

最佳答案

将您的方法更改为:

public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
try {


HttpClient client = getHttpClient();

HttpGet request = new HttpGet();

request.setURI(new URI(url));

HttpResponse response = client.execute(request);

try {
// Get our response as a String.
String jsonString = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(jsonString);
String[] names = JSONObject.getNames(jsonObject);
JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));


// Parse the JSON String into a JSONArray object.
return jsonArray;

} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;

}

关于java.lang.String 无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342792/

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