0

I require a GET reequest from my own server to be extracted from the web and then displayed on screen with a TextView.

I have set up a GET Request.

public class GetMethodEx {


public String getInternetData() throws Exception{
        BufferedReader in = null;
        String data = null;
        try
        {
            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://www.mybringback.com");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) !=null){
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            return data;        
        } finally{
            if (in != null){
                try{
                    in.close();
                    return data;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
}

}

And I have set up on my main thread to extract the information and display it in a text view.

public class Home extends Activity {

    TextView httpStuff;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpexample);
        httpStuff = (TextView) findViewById(R.id.tvhttp);
        GetMethodEx test = new GetMethodEx();
        String returned;
        try {
            returned = test.getInternetData();
            httpStuff.setText(returned);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

However, the Textview doesnt seem to change?

Can someone help me please.

Lyanor Roze
  • 111
  • 1
  • 1
  • 7

2 Answers2

1

Android OS > = 3.0

does not allow NetworkRequest on main UI thread.

Use AsyncTask to call webrequest.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • i just want to conform or any reference if `Android OS < 3.0` is allow any log running operation in UI Thread or Netwrok Opertion on Ui thread – ρяσѕρєя K Nov 30 '12 at 17:36
  • [NetworkOnMainThreadException](http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html) is introduce in API 11 so how it can came in 10. Infect I have test it myself. Android 2.x allow `Network request` on main thread. – Mohsin Naeem Nov 30 '12 at 17:39
  • @imrankhan infect it is written `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. ` – Mohsin Naeem Nov 30 '12 at 17:41
  • but [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute%28Result%29) introduce in API Level 3 to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. because before it it's depend on programmer to develop own logic to avoid hanging of UI or run long running operation in bg – ρяσѕρєя K Nov 30 '12 at 17:46
  • but they make it compulsory for android 3.0 – Mohsin Naeem Nov 30 '12 at 17:47
1

Change your code using AsyncTask if you want to make any network operation from Ui Thread as:

public class Home extends Activity {

    TextView httpStuff;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpexample);
        httpStuff = (TextView) findViewById(R.id.tvhttp);
       new LongOperation().execute("");
    }
private class LongOperation extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
        GetMethodEx test = new GetMethodEx();
        String returned;
        try {
            returned = test.getInternetData();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            return returned;
      }      

      @Override
      protected void onPostExecute(String result) {    
        // Update Ui here    
         httpStuff.setText(result);       
      }

}

}

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213