gpt4 book ai didi

android - JSON 文件未下载,函数返回 null

转载 作者:行者123 更新时间:2023-11-29 22:18:00 25 4
gpt4 key购买 nike

我正在尝试从 google book API 下载一些 JSON。

我正在使用的 URL 和 API key 似乎有效,因为我可以使用浏览器手动获取它。我调用这个类并使用这个函数向它传递一个可用的 URL。我试过 post 和 get(你可以看到我在哪里提交了另一个。

当我在调试时,我可以看到它开始下载过程,或者它似乎开始了,但它总是返回 null。它似乎应该工作。对可能发生的事情有什么想法吗?一个权限什么的。(已经添加了INTERNET PERMISSION)

public static class JSONFunctions2 {
public static JSONObject getJSONfromURL(String url){

/*//initialize*/
InputStream is = null;
String result = "";
JSONObject jArray = null;

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

/*DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
HttpResponse getResponse;
getResponse = client.execute(getRequest);
HttpEntity getResponseEntity = getResponse.getEntity();
if (getResponseEntity != null) {
result= EntityUtils.toString(getResponseEntity);
}*/

/*//try parse the string to a JSON object*/
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}

return jArray;
}
}

我想这是 logcat(我对此有点陌生,其中一些位于顶部可能是因为我将手机从 wifi 切换到 reg.Phone Data)

11-06 23:02:23.716: ERROR/InputDispatcher(159): channel '4081c258 com.buddy/com.BuddyListActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
11-06 23:02:23.716: ERROR/InputDispatcher(159): channel '4081c258 com.buddy/com.BuddyListActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-06 23:03:36.616: ERROR/log_tag(4116): Error in http connection javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
11-06 23:03:36.616: ERROR/log_tag(4116): Error converting result java.lang.NullPointerException
11-06 23:04:04.766: ERROR/log_tag(4116): Error parsing data org.json.JSONException: End of input at character 0 of

最佳答案

我通常这样做,可能对你也有用

HttpResponse response = httpClient.execute(httpPost, localContext);     
serverResponseJSON = EntityUtils.toString(response.getEntity());
Log.i("Server Response", serverResponseJSON);

关于android - JSON 文件未下载,函数返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8033305/

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