gpt4 book ai didi

android - 在 Android 的 webservice Rest 中读取 Json。 FileNotFoundException异常

转载 作者:行者123 更新时间:2023-11-30 04:04:24 26 4
gpt4 key购买 nike

有人知道为什么不能在 android 中使用这个 json 吗? urlJson

结果是 FileNotFoundException,但在导航器上有效。

编辑:

public static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;

try{
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader (url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[]chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);

return buffer.toString();
} finally {
if (reader != null)
reader.close();
}

最佳答案

使用 BufferedReader 不是完成此任务的最佳方法。使用库 org.json.*; 将 HttpClient 与以下示例一起使用会更好吗?

HttpClient httpClient = new DefaultHttpClient();

HttpPost post = new HttpPost("http://www.*****.com/json");

// Execute HTTP Post Request
HttpResponse response = httpClient.execute(post);
String respStr = EntityUtils.toString(response.getEntity());

JSONArray respJSON = new JSONArray(respStr);

关于android - 在 Android 的 webservice Rest 中读取 Json。 FileNotFoundException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978412/

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