gpt4 book ai didi

android - 更高 API 上的 NetworkOnMainThreadException

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:42 28 4
gpt4 key购买 nike

只有当我在更高的 API 上进行调试时,我才会强制关闭脚本:16,但是当涉及到 API 时它工作正常:10。这可能是我的项目设置问题吗?

这是对服务器的简单请求,以获取 fragment 中的类别列表。

画廊.java

public class gallery extends Fragment {

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
private ListView storeList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.tab_frag1_layout,
container, false);
storeList = (ListView) inflater.inflate(R.layout.list, null);
return myFragmentView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xxx.xxx.xxx/android_link.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");

String line = "0";
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());
}
// paring data
int ct_id;
String ct_name;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
ct_id = json_data.getInt("brand_id");
ct_name = json_data.getString("series_name");

}
} catch (JSONException e1) {
Toast.makeText(getActivity(), "No Data Found", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}

日志.txt

01-29 03:25:54.653: E/log_tag(4324): Error in http connectionandroid.os.NetworkOnMainThreadException
01-29 03:25:54.653: E/log_tag(4324): Error converting result java.lang.NullPointerException
01-29 03:25:54.653: W/dalvikvm(4324): threadid=1: thread exiting with uncaught exception (group=0xb5e98288)
01-29 03:25:54.653: E/AndroidRuntime(4324): FATAL EXCEPTION: main
01-29 03:25:54.653: E/AndroidRuntime(4324): java.lang.NullPointerException
01-29 03:25:54.653: E/AndroidRuntime(4324): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-29 03:25:54.653: E/AndroidRuntime(4324): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-29 03:25:54.653: E/AndroidRuntime(4324): at org.json.JSONArray.<init>(JSONArray.java:87)
01-29 03:25:54.653: E/AndroidRuntime(4324): at org.json.JSONArray.<init>(JSONArray.java:103)
01-29 03:25:54.653: E/AndroidRuntime(4324): at com.nazartt.angtrading.gallery.onActivityCreated(gallery.java:93)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.os.Handler.handleCallback(Handler.java:615)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.os.Looper.loop(Looper.java:137)
01-29 03:25:54.653: E/AndroidRuntime(4324): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 03:25:54.653: E/AndroidRuntime(4324): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 03:25:54.653: E/AndroidRuntime(4324): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 03:25:54.653: E/AndroidRuntime(4324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 03:25:54.653: E/AndroidRuntime(4324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 03:25:54.653: E/AndroidRuntime(4324): at dalvik.system.NativeStart.main(Native Method)

最佳答案

你有一个 NetworkOnMainThreadException如果您尝试在主 (UI) 线程 (StrictMode) 上执行网络操作,则会在新版本的 Android (3.0+) 上发生。使用 AsyncTask对于您的网络操作,它的设置很简单,并且以合乎逻辑的方式运行(在后台执行某些操作,然后在完成后发布到 UI 线程)。

关于android - 更高 API 上的 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575293/

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