开发者社区> 问答> 正文

这个json安卓上传数据代码怎么写?

public static void chuanzhi()
 {
 Http c = new Http();
 String params = "{'name':'test1','pwd':'123','ruid':'test2'}";
 Eryptogram eryptogram = new Eryptogram();
 params = eryptogram.encryptData(params);
 String d = url + "/api/user/register?params=" + params;
 System.out.println(d);
 System.out.println(c.get(d));
 }

展开
收起
杨冬芳 2016-07-04 18:18:22 2535 0
1 条回答
写回答
取消 提交回答
  • IT从业

    得用post方式可以看http://www.shangxueba.com/jingyan/1846773.html

    给你分享一个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;  
        }
        }
    
    2019-07-17 19:51:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载