Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中访问数据库导致NetworkOnMainThread异常错误_Android_Networkonmainthread - Fatal编程技术网

在Android中访问数据库导致NetworkOnMainThread异常错误

在Android中访问数据库导致NetworkOnMainThread异常错误,android,networkonmainthread,Android,Networkonmainthread,我使用下面的代码从数据库中检索数据。我收到错误android.os.NetworkOnMainThreadException public void select() { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("id",id)); try {

我使用下面的代码从数据库中检索数据。我收到错误android.os.NetworkOnMainThreadException

 public void select()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("id",id));

    try
    {
        Toast.makeText(getApplicationContext(), "inside try block",Toast.LENGTH_LONG).show();
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://129.107.239.106/select.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        Toast.makeText(getApplicationContext(), "entity ",Toast.LENGTH_LONG).show();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
}
    catch(Exception e)
{
        Log.e("Fail 1", e.toString());  
        Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
}     }

我在清单文件中提供了访问internet的权限代码。有人能帮我解决这个错误吗。提前感谢。

与异常状态完全相同-您无法在主UI线程上执行网络操作。您发布的代码执行HttpPost。调查这项任务。请参阅此处的参考资料:此处:

您不能在主线程上进行联网,这会冻结UI。您必须在另一个线程或异步任务上执行此操作。下次使用谷歌时,每天会有十几次的回复。