安卓:HTTP请求不上工作4.0 [英] Android: Http request doesn't work on 4.0

查看:190
本文介绍了安卓:HTTP请求不上工作4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试code和它的作品罚款2.2和2.3.3,但它崩溃的4.0。

这个问题似乎是与HTTP请求。任何想法,为什么?

 公共类Rezultat延伸活动{
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity);    // HTTP POST
    InputStream为= NULL;
    尝试{            字符串URL =htt​​p://google.com;
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(URL);
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();
    }赶上(例外五){
            Log.e(log_tag,在HTTP连接错误+ e.toString());
    }
    //响应转换为字符串
    尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                    sb.append(行+\\ n);
            }
            is.close();            结果= sb.toString();    }赶上(例外五){
            Log.e(log_tag,错误转换结果+ e.toString());
    }


解决方案

e.printstacktrace()会告诉你:

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html


  

这是当应用程序试图执行引发的异常
  其主线程联网运行。


  
  

这是只抛出瞄准蜂窝SDK的应用程序或
  更高。针对应用较早的SDK版本都可以做
  网络对他们的主事件循环线程,但它的高度
  灰心。查看文档的设计响应。


 私有类DownloadFromUrlTask​​扩展的AsyncTask<弦乐,太虚,位图> {    在preExecute保护无效(){
        支持mDialog = ProgressDialog.show(ChartActivity.this,请稍候...,检索数据...,真正的);
    }    保护字符串doInBackground(字符串的URL ...){
        //你的所有的网络的东西在这里。
        返回结果
    }
}

I tested this code and it works fine on 2.2 and 2.3.3, but it crashes on 4.0.

The problem seems to be with the http request. Any ideas why?

public class Rezultat extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    //http post
    InputStream is=null;
    try{

            String url="http://google.com";
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }
    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();

    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

解决方案

e.printstacktrace() will tell you:

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

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. See the document Designing for Responsiveness.

private class DownloadFromUrlTask extends AsyncTask<String, Void, Bitmap> {

    protected void onPreExecute() {
        mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    protected String doInBackground(String... urls) {
        //All your network stuff here.
        return result
    }
}

这篇关于安卓:HTTP请求不上工作4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆