Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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
Java android.os.NetworkOnMainThreadException android调用Web服务_Java_Android_Http - Fatal编程技术网

Java android.os.NetworkOnMainThreadException android调用Web服务

Java android.os.NetworkOnMainThreadException android调用Web服务,java,android,http,Java,Android,Http,哇 我真的需要帮助,为什么我的代码会这样? 这是我的代码: HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URL); try{ HttpResponse response = httpClient.execute(httpPost); String jsonResult = convertStreamToSt

哇 我真的需要帮助,为什么我的代码会这样? 这是我的代码:

HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);    
try{
            HttpResponse response = httpClient.execute(httpPost);
            String jsonResult = convertStreamToString((response.getEntity().getContent())).toString();
            JSONObject obj = (JSONObject) new JSONTokener(jsonResult).nextValue();
            JSONObject obj2 = obj.getJSONObject("GetRingkasObjekPajak_SingleResult");
            String nameWP = obj2.getString("NM_WP");
            TextView tv = (TextView)findViewById(R.id.dummy_text_three);
            tv.setText(jsonResult);
        }catch (MalformedURLException e) {
            // URL is invalid
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("url invalid");
        } catch (SocketTimeoutException e) {
            // data retrieval or connection timed out
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("RTO");
        } catch (IOException e) {
            // could not read response body 
            // (could not create input stream)
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("couldnt read response");
        } catch (JSONException e) {
            // response body is no valid JSON string
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("json response fail");
        }catch (Exception e) {
            // TODO: handle exception
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText(e.toString());
        }
我还添加了互联网许可

请帮帮我
如何改进我的代码,这样这个问题就解决了。

您不能从主UI线程执行网络操作。您需要在不同的线程中执行与网络相关的任务。更好地利用

根据

当应用程序尝试执行异常时引发的异常 在其主线程上进行网络操作

这仅适用于针对蜂巢SDK或 较高的。允许针对早期SDK版本的应用程序执行以下操作 在他们的主事件循环线程上建立网络,但它非常复杂 气馁


无法从主UI线程执行网络操作。您需要在不同的线程中执行与网络相关的任务。更好地利用

根据

当应用程序尝试执行异常时引发的异常 在其主线程上进行网络操作

这仅适用于针对蜂巢SDK或 较高的。允许针对早期SDK版本的应用程序执行以下操作 在他们的主事件循环线程上建立网络,但它非常复杂 气馁


NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常

Android开发者网站上有一篇关于无痛线程的文章,这是一篇很好的介绍,它将为您提供比实际情况更好的答案

AsyncTask
中运行代码


您可以通过一个很好的示例来了解asyncTask。networkMainThreadException(网络维护线程异常):当应用程序尝试在其主线程上执行网络操作时引发的异常

Android开发者网站上有一篇关于无痛线程的文章,这是一篇很好的介绍,它将为您提供比实际情况更好的答案

AsyncTask
中运行代码


你可以通过一个很好的例子来了解asyncTask。这里是一个asyncTask的例子。。。希望对你有帮助

  private class YourAsyncTaskClass extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
           //your http network call here.
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {
           //update your ui here
      }

      @Override
      protected void onPreExecute() {
             //do any code before exec
      }

      @Override
      protected void onProgressUpdate(Void... values) {
              //If you want to update a progress bar ..do it here
      }
}   

下面是一个异步任务的示例。。。希望对你有帮助

  private class YourAsyncTaskClass extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
           //your http network call here.
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {
           //update your ui here
      }

      @Override
      protected void onPreExecute() {
             //do any code before exec
      }

      @Override
      protected void onProgressUpdate(Void... values) {
              //If you want to update a progress bar ..do it here
      }
}   

在aynctask中调用您的Web服务

private class NetworkTask extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
         // call your network operation here
      }      




} 
私有类NetworkTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在这里呼叫您的网络运营部
}      
} 

在aynctask中调用您的Web服务

private class NetworkTask extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
         // call your network operation here
      }      




} 
私有类NetworkTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在这里呼叫您的网络运营部
}      
} 

它的n/w on main thread异常将您的网络任务转移到asynctasks中在后台执行它将从3.o设备显示此异常您可以通过删除严格模式策略来消除该异常,但最好移动到async task挂起!让我帮你搜索一下!它的n/w on main thread异常将您的网络任务转移到asynctasks中在后台执行它将从3.o设备显示此异常您可以通过删除严格模式策略来消除此异常,但最好移动到async task挂起!让我帮你搜索一下!我希望有任何例子这是非常好的…我从同一个中学到的。我希望有任何例子这是非常好的…我从同一个中学到的。