方法call.enqueue()不调用。而且我放弃了为什么?请帮忙。
我将Retrofit2用于POST请求。这是我简单的项目示例。
我有名为FaceAPI类的API服务。码:
package com.facelocation.testretrofit;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface FaceAPI {
@POST("api/auth/register")
Call<ResponseBody> registerUser(
@Body RegistrationBody body
);
}
我有一个RegistrationBody类,我正在尝试以JSON格式(仅两个参数-电子邮件和密码)将其推入服务器。码:
package com.facelocation.testretrofit;
public class RegistrationBody {
public String email;
public String password;
public RegistrationBody(String email, String password) {
this.email = email;
this.password = password;
}
}
我只有一个按钮,拥有最简单的MainActivity类。码:
package com.facelocation.testretrofit;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
String TAG = "Reg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://face-location.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
FaceAPI api = retrofit.create(FaceAPI.class);
RegistrationBody reg = new RegistrationBody("[email protected]", "password12");
Call<ResponseBody> call = api.registerUser(reg);
Log.i(TAG, "Works until here");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(MainActivity.this, "Все прошло хорошо",Toast.LENGTH_SHORT).show();
Log.i(TAG, "THIS METHOD DOESN'T CALL! WHY?" + response.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
});
}
}
当我运行这段代码时,我没有任何错误。而且我没有任何服务器响应。这行代码适用于Log.i(TAG,“直到这里工作”);但是我认为下一个方法不会调用。我不明白为什么我没有收到任何错误或服务器响应。请帮忙!
最佳答案
因此,问题主要出在您的RegistrationBody()
中。
首先将RegistrationBody()
更改为String
您可以自由使用以下代码来实现:
public static String modelAsString(Object obj) {
String value;
ObjectMapper mapper = new ObjectMapper();
try {
value = mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return e.getMessage();
}
return value;
}
看起来像
RegistrationBody reg = new RegistrationBody("[email protected]", "password12");
String modelAsString = modelAsString(req)
并在活动中添加您的mediaType
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
之后,使您的requestModel如下所示:
RequestBody requestBody = RequestBody.create(JSON, modelAsString);
不要忘记!来自okhttp3的此请求正文,
这意味着您必须
import okhttp3.RequestBody
;毕竟让您喜欢
Call<ResponseBody> call = api.registerUser(requestBody);
让我知道它是否有效!!!!