gpt4 book ai didi

android - 代码在 Jelly Beans 中不起作用

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

我有这段代码从 url 返回 Json 对象。它在 froyo 中工作正常,但相同的代码在 Jelly Beans 中不起作用。

我的代码有问题吗?

请帮帮我,这是我的代码

    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());
}

// 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;
}

这是我的日志错误

11-01 11:01:48.437: E/log_tag(1044): http 连接错误 android.os.NetworkOnMainThreadException
11-01 11:01:48.437: E/log_tag(1044): 错误转换结果 java.lang.NullPointerException
11-01 11:01:48.437: E/log_tag(1044): 解析数据时出错 org.json.JSONException: 在

的字符 0 输入结束

最佳答案

从 Android API v15 开始,它不需要在主线程上运行繁重的进程。所以你应该将你的逻辑移动到另一个线程,比如下面的源代码:

new Thread(new Runnable() {
public void run() {
// your logic
}
}).start();

更多信息请引用 http://developer.android.com/guide/practices/responsiveness.html

关于android - 代码在 Jelly Beans 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171435/

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