0

whoaa i really need help, why my code result like that? this is my code :

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

i also have added internet permission

please help me how to improve my code, so this problem solved.

yozawiratama
  • 4,209
  • 12
  • 58
  • 106
  • its n/w on main thread exception shift your network task inside an asynctasks do in background it will show this exception from3.o devices you can get rid of the exception by removing strict mode policy but its better to move to an async task – Athul Harikumar Jun 04 '13 at 04:57
  • 2
    Hang on! *Let me Google that for you!* http://bit.ly/18KwgKT – Swayam Jun 04 '13 at 05:28

4 Answers4

3

you can not perform network operations from main UI thread. you need to do networking related tasks in different thread. Better use AsyncTask

According to the doc

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

stinepike
  • 54,068
  • 14
  • 92
  • 112
3

Here is an example of Async task... Hope it will be helpfull to you.

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

Finally call this class from anywhere you want like..

  new YourAsyncTaskClass().execute();
amalBit
  • 12,041
  • 6
  • 77
  • 94
0

NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.

There is an article about Painless Threading on the Android developer site which is a good introduction to this, and will provide you with much better depth of answer than can be realistically provided here.

Run your code in AsyncTask.

You can learn about asyncTask here is best explanation with good example .

Harshid
  • 5,701
  • 4
  • 37
  • 50
0

call your webservice inside aynctask.

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

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




} 
URAndroid
  • 6,177
  • 6
  • 30
  • 40