android发送http get请求

android发送http get请求的方法 

String uriAPI = “http://www.getideas.cn“;

  /* 建立HTTP Get对象 */   HttpGet httpRequest = new HttpGet(uriAPI);   try   {    /* 发送请求并等待响应 */    HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);    /* 若状态码为200 ok */

   if (httpResponse.getStatusLine().getStatusCode() == 200)    {     String strResult = EntityUtils.toString(httpResponse       .getEntity());     /* 去没有用的字符 */     strResult = eregi_replace(“(\r\n|\r|\n|\n\r)”, “”, strResult);     mTextView1.setText(strResult);    }    else    {     mTextView1.setText(“Error Response: ”       + httpResponse.getStatusLine().toString());    }   }

  catch (ClientProtocolException e)   {    mTextView1.setText(e.getMessage().toString());    e.printStackTrace();   }

  catch (IOException e)   {    mTextView1.setText(e.getMessage().toString());    e.printStackTrace();   }   catch (Exception e)   {    mTextView1.setText(e.getMessage().toString());    e.printStackTrace();   }

posted @ 2012-07-16 14:11  悬崖上的蒲公英  阅读(275)  评论(0)    收藏  举报