资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android访问WEBAPI,传递json

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


文章名称:Android访问WEBAPI,传递json
路径分享:http://cdkjz.cn/article/godjho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220