我愿意从 http 请求中读取一个简单的 json 文件。这是我试图阅读的 URL:
http://tccdahora.servehttp.com/teste.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() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
Log.e("", result);
// transformando em uma array de objetos JSON
jArray = new JSONArray(result);
return jArray;
}
我做错了什么吗?我收到了错误的请求错误,并且帖子中的字符串返回了一个通知错误的 HTML 代码,而不是我的浏览器显示的 json。
非常感谢您的关注!