如何在retorift2.0中从Response <responsebody>响应中打印json数组

时间:2017-11-16 04:37:16

标签: android json retrofit2 okhttp

我使用retorfit 2.0开发了android应用程序。我在我们的应用程序中发推文。我在浏览器中检查时从url获取json数组格式数据。我在我的应用程序中得到响应,如代码:200ok和responce就像:repoces @ 012345作为原始数据打印。如何以数组格式打印响应。

&#13;
&#13;
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://cdn.syndication.twimg.com/")
                .addConverterFactory(GsonConverterFactory.create())

                .build();

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);


        Call<ResponseBody> response = requestInterface.gettwetter("vinodh654");

        response.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d("Tweeter Respose",response.code()+" "+response.message()+" "+response.body().toString());


            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("Tweeter Respose","Fail");
            }
        });
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

获取JSON使用

response.body().string()

答案 1 :(得分:0)

您可以使用response.body().string()进行打印,如下所示:

  response.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
            String data = response.body().string(); // It will get the response 
            Log.d("Tweeter Respose",response.code()+" "+response.message()+" "+data;
     }

     @Override
     public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.d("Tweeter Respose","Fail");
     }
  });

答案 2 :(得分:0)

试试这个

 @Override
         public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
                String responseString = response.body().string(); 
                Log.d("Response",response.code()+" "+response.message()+" "+data;
         }

答案 3 :(得分:0)

您可以像这样打印json响应 - new Gson().toJson(response.body())

response.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
            String data = new Gson().toJson(response.body()); // It will get the response 
            Log.d("Response",response.code()+" "+response.message()+" "+data;
     }

     @Override
     public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.d("Response","Fail");
     }
  });