love_register 2015-02-04 01:32 采纳率: 100%
浏览 1752
已采纳

小白求助,很迷茫Android中写post请求,这个应该怎么写那

如图,他们提供的数据接口是这样说的,图片说明
于是我就这么写的,不对啊,该怎么写那图片说明

  • 写回答

3条回答 默认 最新

  • super小靖 2015-02-06 10:50
    关注

    给你分享一个我的android访问http的类,超级好用,从来没让我担心过

     public class MyHttpClient {
        /** 
         * 通过HttpClient发送GET请求 
         * @param path 请求路径 
         * @param params 请求参数 
         * @param ecoding 请求编码 
         * @return 请求是否成功 
         */  
        public HttpResponse sendHttpClientGETRequest(String path,Map<String, String> params, String ecoding) throws Exception {  
            StringBuilder url=new StringBuilder(path);  
            url.append("?");  
            for (Map.Entry<String, String> entry : params.entrySet()) {  
                url.append(entry.getKey()).append("=");  
                url.append(URLEncoder.encode(entry.getValue(),ecoding));  
                url.append("&");  
            }  
            url.deleteCharAt(url.length()-1);  
            HttpGet httpGet=new HttpGet(url.toString());  
            DefaultHttpClient client=new DefaultHttpClient();  
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
            HttpResponse response=client.execute(httpGet); 
            if(response.getStatusLine().getStatusCode()==200){  
                return response;  
            }  
            return null;  
        } 
    
            /** 
        * 通过HttpClient发送Post请求 
        * @param path 请求路径 
        * @param params 请求参数 
        * @param ecoding 请求编码 
        * @return 请求是否成功 
        */  
        public HttpResponse sendHttpClientPOSTRequest(String path,  
               Map<String, String> params, String ecoding) throws Exception {  
           List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放请求参数  
           if(params!=null && !params.isEmpty()){  
               for (Map.Entry<String, String> entry : params.entrySet()) {  
                   pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
               }  
           }  
           UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
           HttpPost httpPost=new HttpPost(path);  
           httpPost.setEntity(enFormEntity);  
           DefaultHttpClient client=new DefaultHttpClient();  
           client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
           client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
           HttpResponse response=client.execute(httpPost);  
           if(response.getStatusLine().getStatusCode()==200){  
               return response;  
           } 
           return null;  
        }
    
        /** 
        * 通过HttpClient发送Post请求 
        * @param path 请求路径 
        * @param params 请求参数 
        * @param ecoding 请求编码 
        * @return 请求是否成功 
        */  
        public HttpResponse sendHttpClientPOSTRequest(String path,  
               Map<String, String> params, String ecoding, int timeout) throws Exception {  
           List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放请求参数  
           if(params!=null && !params.isEmpty()){  
               for (Map.Entry<String, String> entry : params.entrySet()) {  
                   pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
               }  
           }  
           UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
           HttpPost httpPost=new HttpPost(path);  
           httpPost.setEntity(enFormEntity);  
           DefaultHttpClient client=new DefaultHttpClient();  
           client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
           client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
           HttpResponse response=client.execute(httpPost);  
           if(response.getStatusLine().getStatusCode()==200){  
               return response;  
           } 
           return null;  
        }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试