在进行改造 2 发布请求时,响应正文为空

Posted

技术标签:

【中文标题】在进行改造 2 发布请求时,响应正文为空【英文标题】:While making a retrofit2 post request, response body is null 【发布时间】:2018-02-16 14:57:44 【问题描述】:

我在发出 POST 请求时将响应正文设为空。我在请求正文中传递数据。我正在使用 Retrofit2 拨打电话。我已经复制了相关代码sn-ps你能帮我解决这个问题吗:

//JSON 正文:


  "name" : "Anubhav Arora",
  "email" : "anubhavarora02@gmail.com",
  "password" : "anubhavpass123"

//响应:


  "message": "User Registered Successfully !!",
  "email": "anubhavarora02@gmail.com"

//终点:

@POST("registeruser")
Call<UserRegistration> registerUser(@Body User user);

//发布方法调用

private void registerUser(String username, String email, String 
password) 
    User user = new User(username, email, password);

    UserEndPoints apiService = APIClient.getClient()
            .create(UserEndPoints.class);

    Call<UserRegistration> call = apiService.registerUser(user);
    call.enqueue(new Callback<UserRegistration>() 
        @Override
        public void onResponse(Call<UserRegistration> call, Response<UserRegistration> response) 
            Toast.makeText(RegisterActivity.this, response.body().getEmail(), Toast.LENGTH_SHORT).show();
        

        @Override
        public void onFailure(Call<UserRegistration> call, Throwable t) 
            Log.d("TAG", t.toString());
        
    );

//用户类

public class User 

private String name, email, password;

public User(String name, String email, String password) 
    this.name = name;
    this.email = email;
    this.password = password;


public String getName() 
    return name;


public void setName(String name) 
    this.name = name;


public String getEmail() 
    return email;


public void setEmail(String email) 
    this.email = email;


public String getPassword() 
    return password;


public void setPassword(String password) 
    this.password = password;

//用户注册类

public class UserRegistration 

@SerializedName("message")
@Expose
private String message;
@SerializedName("email")
@Expose
private String email;

public String getMessage() 
    return message;


public void setMessage(String message) 
    this.message = message;


public String getEmail() 
    return email;


public void setEmail(String email) 
    this.email = email;



//API 客户端

public class APIClient 

//10.0.2.2
public static final String BASE_URL = "http://10.0.2.2:3000/api/v1/";
private static Retrofit retrofit;

public static Retrofit getClient() 
    if (retrofit == null) 
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
    return retrofit;


【问题讨论】:

【参考方案1】:

您需要添加基本网址

Retrofit.Builder builder = new Retrofit.Builder()
       .baseUrl(url)
       .addConvertorFactory(new GsonConvertorFactory.create());


Retrofit retrofit = builder.build();

UserEndPoints apiService = retrofit.create(UserEndPoints.class);
Call<UserRegistration> call = apiService.registerUser(user);

call.enque ......

【讨论】:

我已经在 APIClient 类中添加了。

以上是关于在进行改造 2 发布请求时,响应正文为空的主要内容,如果未能解决你的问题,请参考以下文章

改造 2:即使有也没有请求正文

改造2如何处理有时无效有时非无效的服务器响应

AJAX 请求正文为空(无 jQuery)

标头传输编码:分块并请求为空

AFNetworking 2 - 请求失败时没有响应正文

如何在改造请求的正文中发布原始的整个 JSON?