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());   //获取字符串

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM