darling_shadow吧 关注:26贴子:1,642
  • 8回复贴,共1

Android开发中如何执行POST请求.......

只看楼主收藏回复

...


1楼2012-07-02 18:15回复
    这篇文章我们探讨下在Android开发中如何执行一个Post请求。
    首先我们先了解下Get请求和Post请求的区别:
    表单提交中get和post方式的区别有5点
    1.get是从服务器上获取数据,post是向服务器传送数据。
    2.get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTPpost机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
    3.对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
    4.get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
    5.get安全性非常低,post安全性较高。
    


    2楼2012-07-02 18:16
    回复
      那么接下来让我们看看在Android平台开发中如何执行一个Post请求:
      Java代码
      String uriAPI = "http://192.168.1.100:8080/test/test.jsp"; //这是我测试的本地,大家可以随意改
      /*建立HTTPost对象*/
      HttpPost httpRequest = new HttpPost(uriAPI);
      /*
      * NameValuePair实现请求参数的封装
      */
      List <NameValuePair> params = new ArrayList <NameValuePair>();
      params.add(new BasicNameValuePair("u", "沈大海"));
      params.add(new BasicNameValuePair("p", "123"));
      try
      {
      /* 添加请求参数到请求对象*/
      httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
      /*发送请求并等待响应*/
      HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
      /*若状态码为200 ok*/
      if(httpResponse.getStatusLine().getStatusCode() == 200)
      {
      /*读返回数据*/
      String strResult = EntityUtils.toString(httpResponse.getEntity());
      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();
      }
      


      3楼2012-07-02 18:16
      回复
        那么接下来让我们看看在Android平台开发中如何执行一个Post请求:
        Java代码
        String uriAPI = " h t t p ://192.168.1.100:8080/test/test.jsp"; //这是我测试的本地,大家可以随意改
        /*建立HTTPost对象*/
        HttpPost httpRequest = new HttpPost(uriAPI);
        /*
        * NameValuePair实现请求参数的封装
        */
        List <NameValuePair> params = new ArrayList <NameValuePair>();
        params.add(new BasicNameValuePair("u", "沈大海"));
        params.add(new BasicNameValuePair("p", "123"));
        try
        {
        /* 添加请求参数到请求对象*/
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        /*发送请求并等待响应*/
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
        /*若状态码为200 ok*/
        if(httpResponse.getStatusLine().getStatusCode() == 200)
        {
        /*读返回数据*/
        String strResult = EntityUtils.toString(httpResponse.getEntity());
        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();
        }
        


        4楼2012-07-02 18:17
        回复
          HttpClient httpclient=new HttpClient();
          HttpPost httppost=new HttpPost("http://ssssss.com/sign");
          String s="";
          String ss="";
          List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
          nameValuePairs.add(new BasicNameValuePair("content", s+",ss"+ss));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response;
          response=httpclient.execute(httppost);
          System.out.println(response.toString());


          5楼2012-07-02 18:18
          回复
            HttpClient httpclient=new HttpClient();
            HttpPost httppost=new HttpPost("h t t p://ssssss.com/sign");
            String s="";
            String ss="";
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("content", s+",ss"+ss));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response;
            response=httpclient.execute(httppost);
            System.out.println(response.toString());


            6楼2012-07-02 18:18
            回复
              import org.apache.http.HttpResponse;
              import org.apache.http.NameValuePair;
              import org.apache.http.client.HttpClient;
              import org.apache.http.client.entity.UrlEncodedFormEntity;
              import org.apache.http.client.methods.HttpPost;
              import org.apache.http.impl.client.DefaultHttpClient;
              import org.apache.http.message.BasicNameValuePair;
              倒包有问题,会报错,唉,心情不好,等有时间吧。。。


              7楼2012-07-02 18:19
              回复
                /** * */package com.jiuchongju.util;
                import java.util.ArrayList;
                import java.util.List;import java.util.Map;
                import org.apache.http.HttpResponse;
                import org.apache.http.NameValuePair;
                import org.apache.http.client.HttpClient;
                import org.apache.http.client.entity.UrlEncodedFormEntity;
                import org.apache.http.client.methods.HttpGet;
                import org.apache.http.client.methods.HttpPost;
                import org.apache.http.impl.client.DefaultHttpClient;
                import org.apache.http.message.BasicNameValuePair;
                import org.apache.http.protocol.HTTP;
                import org.apache.http.util.EntityUtils;/** * Description:
                * @version 1.0 */
                public class HttpUtil{
                // 创建HttpClient对象
                public static HttpClient httpClient ;
                public static final String BASE_URL = "h t tp://www.xxx.com/projectname/name_baidu!jsonstring";
                /** * * @param url 发送请求的URL * @return 服务器响应字符串 *
                @throws Exception */
                public static String getRequest(String url) throws Exception {
                httpClient= new DefaultHttpClient();
                try{
                // 创建HttpGet对象。
                HttpGet get = new HttpGet(url);
                // 发送GET请求
                HttpResponse httpResponse = httpClient.execute(get);
                // 如果服务器成功地返回响应
                if (httpResponse.getStatusLine() .getStatusCode() == 200) {
                // 获取服务器响应字符串
                String result = EntityUtils .toString(httpResponse.getEntity());
                return result;
                }
                }
                catch(Exception e){
                e.printStackTrace();
                return "获取数据失败!";
                }finally{
                httpClient.getConnectionManager().shutdown();
                }
                return null;
                }
                


                8楼2012-07-02 18:24
                回复
                  /**
                  *
                  * @param url 发送请求的URL
                  * @param params 请求参数
                  * @return 服务器响应字符串
                  * @throws Exception
                  */
                  public static String postRequest(String url , Map<String ,String> rawParams) {
                  httpClient= new DefaultHttpClient();
                  try{
                  // 创建HttpPost对象。
                  HttpPost post = new HttpPost(url);
                  // 如果传递参数个数比较多的话可以对传递的参数进行封装
                  List<NameValuePair> params = new ArrayList<NameValuePair>();
                  for(String key : rawParams.keySet()) {
                  //封装请求参数
                  params.add(new BasicNameValuePair(key , rawParams.get(key)));
                  }
                  // 设置请求参数
                  post.setEntity(new UrlEncodedFormEntity( params,HTTP.UTF_8));
                  // 发送POST请求
                  HttpResponse httpResponse = httpClient.execute(post);
                  // 如果服务器成功地返回响应
                  if (httpResponse.getStatusLine() .getStatusCode() == 200) {
                  // 获取服务器响应字符串
                  String result = EntityUtils .toString(httpResponse.getEntity());
                  return result;
                  }
                  }catch(Exception e){
                  e.printStackTrace();
                  }finally{
                  httpClient.getConnectionManager().shutdown();
                  }
                  return null;
                  }


                  9楼2012-07-02 18:32
                  回复