当我运行这个程序时,我得到了这个错误。我不知道怎么解决。请帮我找到它。
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;
}
}
}
您不能将字符串强制转换为JsonArray
,首先将其强制转换为JsonObject
try {
JSONObject jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
然后从中获取JsonArray
jObj。getJSONArray(名称)
将您的方法更改为:
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;
}
尝试如下:您可以按如下方式解析您的响应:
// Get our response as a String.
String jsonString = EntityUtils.toString(response.getEntity());
JSONObject m_jobj;
try {
m_jobj = new JSONObject(jsonString);
JSONArray m_ja = m_jobj.getJSONArray("cityData");
for (int i = 0; i < m_ja.length(); i++)
{
JSONObject m_obj = m_ja.getJSONObject(i);
String city=m_obj.getString("cityID");
String cityName=m_obj.getString("cityName");
//And so on get all the values.
}
更新您的getJSONArrayFromurl()
,如下所示:
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 m_jobj;
try {
m_jobj = new JSONObject(jsonString);
JSONArray m_ja = m_jobj.getJSONArray("cityData");
/*for (int i = 0; i < m_ja.length(); i++)
{
JSONObject m_obj = m_ja.getJSONObject(i);
String city=m_obj.getString("cityID");
String cityName=m_obj.getString("cityName");
//And so on get all the values.
}*/
// Parse the JSON String into a JSONArray object.
return m_ja;
} 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;
}
结果: Main.java:22:错误:不兼容的类型:字符串不能转换为T返回Y;其中T是类型变量:T扩展类Vehicle 1错误中声明的字符串
我的代码有一个错误。 我的适配器类: 我的班级:
大家好,我的问题是我为android开发了一个应用程序,它使用json解析器从我的php Web服务中获取数据并将其显示在listview中,但当我运行该应用程序时,它显示了以下内容: 日志: 我的代码: 具有列表视图的活动并调用json解析器: Json解析器在这个我只改变了这一行BufferedReader=new BufferedReader(new InputStreamReader(is
在我的项目中,我有这样一个枚举: 我有这个代码: 我有个例外 myMap由数据库中的数据填充,知道它是SQL Server数据库,并且从数据库返回的myKey在数据库中是tinyint类型。 你能告诉我我做错了什么吗?谢谢 当做
我有一个12个月温度的文本文件。但是当我试图找到平均温度时,我得到的错误是“String conly be converty to int”(字符串不能转换为int temp[counter]=sc.nextline(); 有人能说出怎么了吗?