Im willing to read a simple json file from a http request. This is the URL im tryng to read:
http://tccdahora.servehttp.com/state.php" rel=“no follow'>http://tccdahora.servehttp.com/state.php
(笑声) 因此,我找到了一种方法将一个 URL 转换成JSON的方法, 但是它不起作用, 尝试了许多不同的方法, 或者让一个头头上岗, 但是没有成功。
这是方法 :
public static JSONArray getPostJSONObject(String url) throws Throwable {
// faz o POST na pagina php e obtem o inputstream de resposta
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url); // "http://tccdahora.servehttp.com/teste.php"
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader("host", url);
//httppost.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
// transforma o que o php printar em uma strigzona
JSONArray jArray;
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "
");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
is.close();
String result = sb.toString();
Log.e("", result);
// transformando em uma array de objetos JSON
jArray = new JSONArray(result);
return jArray;
}
是否我做错了什么? Im realize 错误请求错误, 文章的字符串正在返回 HTML 代码, 而不是我浏览器显示的 Json 。
非常感谢大家的注意!