json嵌套获取

目前思路

获取string

string转化为json

json在变为string

假如json为{“code”:200,“info”:{“token”:一大堆啥,"accid":一大堆啥}}

代码没写全,只是个参考,主要是想记录一下httpClient一般使用情况,外加json转变问题

不过现在不怎么用,一般用okHttp,但我是小白,对于一些服务器官网上用的这个httpClient,本来想用Okhttp连接,但总是报错参数错误,就没有用Okhttp

        //POST       
        //获取HttpClient对象
        HttpClient httpClient = HttpClientBuilder.create().build();

        //设置url
        String url = "";
        HttpPost httpPost = new HttpPost(url);

        // 设置请求的header
        httpPost.addHeader("关键字", "值");

        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("关键字", "值");
        //nvps.add(new BasicNameValuePair("关键字", "值");

       
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));   

        
       HttpResponse response = httpClient.execute(httpPost);
       String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");

       /*依赖com.gexin.fastjson.JSON;这里不用这方法,换一种,依赖net.sf.json.JSONObject
       
       String code = JSON.parseObject(responseEntity).getString("code");
       String info = JSON.parseObject(responseEntity).getString("info");
       System.out.println(responseEntity);
        */
       JSONObject jsonObject = JSONObject.fromObject(responseEntity);
       String info = jsonObject.getString("关键字");
       String code = jsonObject.getString("关键字");
       jsonObject = JSONObject.fromObject(info);
       String token =jsonObject.getString("token");
       //System.out.println(code+"  "+info+" "+token+"  "+responseEntity);

       //判断是否发送成功,发送成功输出
       /*if (code=="200") {
            System.out.println("OK");
        }*/
  

//GET
//获取HttpClient对象
HttpClient httpClient = HttpClientBuilder.create().build();

//设置参数,假如为pairs
...

//还可以通过构造buider
URIBuilder builder = new URIBuilder(url)
                     .setParameters(pairs);
HttpGet get = new HttpGet(builder.build());

//执行
response = httpClient.execute(get);

//得到实体,这里简单些,不判断是否成功
String result = entityToString(reponse.getEntity);

OkHttp连接

//POST方法
//拿到OkHttpClient对象
OKHttpClient okHttpClient = new OKHttpClient();

//创建requesBody
MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
RequestBody requestbody = RequestBody.create(mediaType,json);

//构造request
Request request = new Request.Builder()
                  .url(....)
                  .addheader(....)
                  .post(requestBody)
                  .build();

//执行call
client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

//Get方法
//拿到OkHttpClient对象
OKHttpClient okHttpClient = new OKHttpClient();

//构造request
Request request = new Request.Builder()
                  .get()
                  .url(....)
                  .addheader(....)
                  .build();
//执行call
client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

感觉还是OKhttp好用一些

猜你喜欢

转载自blog.csdn.net/a_higher/article/details/91126307
今日推荐