0

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

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

我还添加了互联网许可

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

4

4 回答 4

3

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

根据文档

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

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。

于 2013-06-04T04:57:38.100 回答
3

这是异步任务的示例...希望对您有所帮助。

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

最后从任何你想要的地方调用这个类..

  new YourAsyncTaskClass().execute();
于 2013-06-04T05:10:40.633 回答
0

在 aynctask 中调用您的网络服务。

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

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




} 
于 2013-06-04T05:15:06.427 回答
0

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

Android 开发者网站上有一篇关于Painless Threading此的文章很好地介绍了这一点,并且将为您提供比此处实际提供的更深入的答案。

AsyncTask.

您可以在此处了解 asyncTask ,这是最好的示例说明。

于 2013-06-04T05:08:31.780 回答