Android访问WEBAPI,传递json

发布时间:2020-05-30 21:15:05 作者:武小猪
来源:网络 阅读:6135

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暂时就不贴出来了,需要注意的是并不是通过参数传递的。

推荐阅读:
  1. android json 解析
  2. php传递json给jquery的问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

httpclient android webapi

上一篇:jquery显示和隐藏div特效demo

下一篇:JavaScript初学者必看“this”

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》