Android的http两种请求方式

http的两种请求方式:POST和GET

由于Android的SDK包含org.apache.http包,所以不用导入jar了

GET方式:

String serverURL = "http://127.0.0.1/xxx/xx.jsp?username=abc;
HttpGet httpRequest = new HttpGet(serverURL);// 建立http get联机
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求
if (httpResponse.getStatusLine().getStatusCode() == 200)
    String result = EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串

POST方式:

        String uriAPI = "http://127.0.0.1/xxx/xx.jsp";  //声明网址字符串
        HttpPost httpRequest = new HttpPost(uriAPI);   //建立HTTP POST联机
        List <NameValuePair> params = new ArrayList <NameValuePair>();   //Post运作传送变量必须用NameValuePair[]数组储存 
        params.add(new BasicNameValuePair("str", "I am Post String"));   
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   //发出http请求
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);   //取得http响应
        if(httpResponse.getStatusLine().getStatusCode() == 200)    
          String strResult = EntityUtils.toString(httpResponse.getEntity());   //获取字符串

 

posted @ 2013-04-16 20:02  Marble  阅读(24806)  评论(0编辑  收藏