【问题标题】:Android Programming: HTTP GET REQUEST NOT WORKINGAndroid 编程:HTTP GET 请求不起作用
【发布时间】:2012-11-30 17:14:54
【问题描述】:

我需要从我自己的服务器中提取一个 GET 请求,然后从 Web 中提取,然后使用 TextView 显示在屏幕上。

我已经设置了一个 GET 请求。

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

}

我已经在我的主线程上设置了提取信息并将其显示在文本视图中。

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

但是,Textview 似乎没有改变?

谁能帮帮我。

【问题讨论】:

  • 如果您使用的是 android >=3.0 然后在 AsyncTask 中运行 getInternetData
  • 请您详细说明一下。谢谢。
  • 您使用的是哪个安卓操作系统?
  • 4.2 是我正在运行的模拟器。

标签: android post https get request


【解决方案1】:

如果您想从 Ui Thread 进行任何网络操作,请使用 AsyncTask 更改您的代码:

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 操作系统 > = 3.0

    不允许在主 UI 线程上使用 NetworkRequest

    使用AsyncTask调用webrequest。

    【讨论】:

    • 如果Android OS &lt; 3.0 允许在 UI 线程或 Ui 线程上的 Netwrok 操作中的任何日志运行操作,我只想符合或任何参考
    • NetworkOnMainThreadException 是在 API 11 中引入的,所以它是如何出现在 10 中的。感染我自己测试过。 Android 2.x 允许在主线程上使用Network request
    • @imrankhan infect 写成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.
    • AsyncTask 在 API 级别 3 中引入以执行后台操作并在 UI 线程上发布结果,而无需操作线程和/或处理程序。因为在此之前它依赖于程序员开发自己的逻辑以避免 UI 挂起或在 bg 中运行长时间运行的操作
    • 但他们强制要求 android 3.0
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2020-07-11
    • 2019-08-18
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多