HttpClient网络请求之上传Json字符串及对象解析为Json字符串

/**
 * Gson解析对象为Json字符串
 */
         ParamsModel dataModel = new ParamsModel();
         dataModel.setAccount("admin");
         dataModel.setPassword("admin123");
Gson gson2 = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
String gsonString = gson2.toJson(model);
/**
 * 解析二
 */
HashMap<String, String> params = new HashMap<>();
params.put("key1", "value1");
params.put("key2", "这里是需要提交的json格式数据");
params.put("key3", "也可以使用三方工具将对象转成json字符串");
params.put("key4", "其实你怎么高兴怎么写都行");
JSONObject jsonObject = new JSONObject(params);
String strJson = jsonObject.toString();
/**
 * post请求
 * @param json
 */
public void returnNet(final String json){
    new  Thread(new Runnable() {
        @Override
        public void run() {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost(GlobalConstant.API_HOST);
            post.setHeader("Content-type", "application/json");
            StringEntity postingString = null;// json传递
            try {
                postingString = new StringEntity(json);
                post.setEntity(postingString);
                HttpResponse response = httpClient.execute(post);
                System.out.println(post.toString());
                if(response.getStatusLine().getStatusCode()==200){
                    HttpEntity entity = response.getEntity();
                    String result = EntityUtils.toString(entity, "UTF-8");
                    System.out.println(result);
                    JSONObject json = JSON.parseObject(result);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
/**
     * get请求
     */
    public void returnGet(){
        new  Thread(new Runnable() {
            @Override
            public void run() {
                String uriAPI = "https://www.hao123.com/";
                String result= "";
//      HttpGet httpRequst = new HttpGet(URI uri);
//      HttpGet httpRequst = new HttpGet(String uri);
//      创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。
                HttpGet httpRequst = new HttpGet(uriAPI);
//      new DefaultHttpClient().execute(HttpUriRequst requst);
                try {
                    //使用DefaultHttpClient类的execute方法发送HTTP GET请求,并返回HttpResponse对象。
                    HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);//其中HttpGet是HttpUriRequst的子类
                    System.out.println(httpRequst.toString());
                    if(httpResponse.getStatusLine().getStatusCode() == 200)
                    {
                        HttpEntity httpEntity = httpResponse.getEntity();
                        result = EntityUtils.toString(httpEntity);//取出应答字符串
                        // 一般来说都要删除多余的字符
                        result.replaceAll("\r", "");//去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格
                        Log.e("==result==",result.toString());
                        JSONObject json = JSON.parseObject(result);
                    }
                    else{
                        httpRequst.abort();
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    result = e.getMessage().toString();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    result = e.getMessage().toString();
                }
            }
        }).start();
    }

转载于:https://my.oschina.net/c99k3/blog/886494

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值