How does OkHttp get Json string?

后端 未结 5 1869
不知归路
不知归路 2020-12-02 08:50

Solution: It was a mistake on my side.

The right way is response.body().string() other than response.body.toString

5条回答
  •  眼角桃花
    2020-12-02 09:30

    try {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url(urls[0])
            .build();
        Response responses = null;
    
        try {
            responses = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonData = responses.body().string();
        JSONObject Jobject = new JSONObject(jsonData);
        JSONArray Jarray = Jobject.getJSONArray("employees");
    
        for (int i = 0; i < Jarray.length(); i++) {
            JSONObject object     = Jarray.getJSONObject(i);
        }
    }
    

    Example add to your columns:

    JCol employees  = new employees();
    colums.Setid(object.getInt("firstName"));
    columnlist.add(lastName);           
    

提交回复
热议问题