尝试解析 json 时出现错误。你能帮我吗?我阅读了 json url,但是当我尝试解析 json 时它给了我一个异常。编码:
public String lecturaJsonTusPerlas() {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://www.tvnotas.com.mx/rss/feed/tvn-horoscopo.json");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
return result;
} catch (Exception e) {
Log.d("DEFEKAS","EXCEPCION");
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
要读取 json:
JSONArray jsonArray2 = new JSONArray(lecturaJsonTusPerlas);
for (int i = 0; i < jsonArray2.length(); i++) {
JSONObject jsonObject = jsonArray2.getJSONObject(i);
String atr1 = null;
boolean var1 = jsonObject.getBoolean(atr1);
String atr2 = null;
int var2 = jsonObject.getInt(atr2);
String atr4 = null;
String var3 = jsonObject.getString(atr4);
}
我认为 url 是 json,因为当我尝试使用 google chrome 扩展名进行提取时,我没有任何问题。