码迷,mamicode.com
首页 > 移动开发 > 详细

Android访问WEBAPI,传递json

时间:2016-04-19 20:22:08      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:android   httpclient   webapi   

public static String sendHttpRequestByPost(final String address,String encode,MatterReflectDTO dto){

        String result = "";

        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(address);


        String jsonString = dto.toString();


        try {

            StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(dto));

            System.out.println(stringEntity.toString());

            httpPost.setEntity(stringEntity);

            httpPost.setHeader("Accept", "application/json");

            httpPost.setHeader("Content-type", "application/json");

            httpPost.setHeader("Accept-Encoding", "gzip");


            HttpResponse httpResponse = defaultHttpClient.execute(httpPost);

            System.out.println("请求结果:"+httpResponse.getStatusLine().getStatusCode());

            int code = httpResponse.getStatusLine().getStatusCode();

            if(code ==200){

                InputStream is = httpResponse.getEntity().getContent();

                result = "true";


            }


        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();  

        } catch (ClientProtocolException e) {

            e.printStackTrace(); 

        } catch (IOException e) {

            e.printStackTrace();  

        }

        return result;

    }

服务器端使用的是.net暂时就不贴出来了,需要注意的是并不是通过参数传递的。

本文出自 “五小猪” 博客,请务必保留此出处http://wuxiaozhu.blog.51cto.com/7942143/1765405

Android访问WEBAPI,传递json

标签:android   httpclient   webapi   

原文地址:http://wuxiaozhu.blog.51cto.com/7942143/1765405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!