O_Casper_ne 2013-06-17 06:27 采纳率: 7.7%
浏览 2275
已采纳

Android: Http request 不能在 4.0上运行

我在 2.2 和 2.3.3 上运行这段代码可以运行,但是在4.0上崩溃。是不是http请求的问题,大家的意见呢?

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

6条回答

  • xiaoyan_12 2013-06-18 08:59
    关注
    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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器