Skip to content Skip to sidebar Skip to footer

Android Programming: HTTP GET REQUEST NOT WORKING

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 { pub

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

}

}


Solution 2:

Android OS > = 3.0

does not allow NetworkRequest on main UI thread.

Use AsyncTask to call webrequest.


Post a Comment for "Android Programming: HTTP GET REQUEST NOT WORKING"