Android Programming: HTTP GET REQUEST NOT WORKING

…衆ロ難τιáo~ 提交于 2019-12-13 07:42:44

问题


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.


回答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);       
      }

}

}




回答2:


Android OS > = 3.0

does not allow NetworkRequest on main UI thread.

Use AsyncTask to call webrequest.



来源:https://stackoverflow.com/questions/13649888/android-programming-http-get-request-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!