2017-08-11 64 views
1

我不确定这是否是一个愚蠢的问题(因此为什么我无法找到答案),但我想知道是否可以Toast或Log输出创建的JSON是在发送时发送到服务器。如何查看使用Retrofit创建的JSON字符串

我只是感兴趣,看看所创建的JSON - 我使用下面的方法,它是利用Retrofit.addConverterFactory(GsonConverterFactory.create(gson))

private void addTeamMember(final List teamMemberArray,final String team_id) { 

    //helps with debugging regarding post requests 
    Gson gson = new GsonBuilder() 
      .setLenient() 
      .create(); 


    //Retrofit is a REST Client for Android and Java by Square. 
    //It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice 
    Retrofit retrofit = new Retrofit.Builder() 
      //directing to the localhost which is defined in the Constants Class as BASE_URL 
      .baseUrl(Constants.BASE_URL) 
      //Add converter factory for serialization and deserialization of objects. 
      //Gson passed as a parameter to help with debug 
      .addConverterFactory(GsonConverterFactory.create(gson)) 
      //Create the Retrofit instance using the configured values. 
      .build(); 



    //The Retrofit class generates an implementation of the RequestInterface interface. 
    RequestInterface requestInterface = retrofit.create(RequestInterface.class); 



    for (Object x : teamMemberArray) { 


     //create new Team object 
     TeamMember teamMember = new TeamMember(); 


     //setter 
     teamMember.setFullName(String.valueOf(x)); 
     teamMember.setTeam_id(team_id); 

     Toast.makeText(getActivity(), teamMember.getFullName(), 
       Toast.LENGTH_LONG).show(); 


     //create new server object 
     final ServerRequest request = new ServerRequest(); 


     //make a request to set the operation to Team_Member 
     request.setOperation(Constants.Team_Member); 
     //set values entered for the new teamMember to be sent to the server 
     request.setTeamMember(teamMember); 


     Call<ServerResponse> response = requestInterface.operation(request); 


     /** 
     * Enqueue is used to Asynchronously send the request and notify callback of its response or if an error occurred 
     * talking to the server, creating the request, or processing the response. 
     */ 


     response.enqueue(new Callback<ServerResponse>() { 

      @Override 
      public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) { 

       ServerResponse resp = response.body(); 


      /*Snackbars provide lightweight feedback about an operation. They show a brief message at the 
      bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other 
      elements on screen and only one can be displayed at a time. 
      */ 


       Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show(); 


       if (resp.getResult().equals(Constants.SUCCESS)) { 
        SharedPreferences.Editor editor = pref.edit(); 


        Log.d("TEST VALUE", "getTeamMemberName() = " + response.body().getTeamMember().getFullName()); 
        Log.d("TEST VALUE", "getTeamMemberUniqueID() = " + response.body().getTeamMember().getUnique_id()); 
        Log.d("TEST VALUE", "getTeamMemberTeamID() = " + response.body().getTeamMember().getTeamID()); 

          editor.putString(Constants.FULL_NAME, resp.getTeamMember().getFullName()); 
          editor.putString(Constants.UNIQUE_ID, resp.getTeamMember().getUnique_id()); 
          editor.putString(Constants.TEAM_ID, resp.getTeamMember().getTeamID()); 
          editor.apply(); 
          goToQuestions(); 

       } 

       progress.setVisibility(View.INVISIBLE); 

      } 

      @Override 
      public void onFailure(Call<ServerResponse> call, Throwable t) { 

       progress.setVisibility(View.INVISIBLE); 
       Log.d(Constants.TAG, "failed" + t); 
       Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show(); 


      } 
     }); 
    } 

} 

我的理由是,如果我能看到JSON被发送过,它在使用Postman时会帮助我进行调试。

+0

ü想要什么和改装的版本,您正在使用 – Anil

回答

3

您可以使用OkHttpInterceptor来记录与Retrofit网络通话。它将拦截任何呼叫并在记录器中显示日志。

添加dependecy在gradle这个:

compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' 

然后之前设置你的改型客户使用:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
logging.setLevel(Level.BASIC); 
OkHttpClient client = new OkHttpClient.Builder() 
    .addInterceptor(logging) 
    .build(); 

最后,在改造补充:

retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .client(client) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
1

dependencies

添加此
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' 

和ApiClient

public class ApiClient { 


    public static final String BASE_URL = "URL"; 
    private static Retrofit retrofit = null; 



    public static Retrofit getClient() { 

     HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
     logging.setLevel(HttpLoggingInterceptor.Level.BODY); 


     final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
       .readTimeout(300, TimeUnit.SECONDS) 
       .connectTimeout(300, TimeUnit.SECONDS) 
       .addInterceptor(logging) 
       .build(); 


     if (retrofit == null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .client(okHttpClient) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 
    } 

} 
相关问题