[英]Call Enqueue function in Retrofit
这是我第一次使用 Retrofit2 并且我反复收到 response.isSuccessful() == FALSE 这是我的代码:
public void checkLogin(final String username , final String password) {
mAPIService.userLogIn(username,password).enqueue(new Callback<CheckUser>() {
@Override
public void onResponse(Call<CheckUser> call, retrofit2.Response<CheckUser> response) {
if(response.isSuccessful()) {
Log.i(TAG, "post submitted to API." + response.body());
if(response.body().getSucces()) {
Intent i = new Intent(getApplicationContext(),MainActivityTab.class);
startActivity(i);
finish();
/*Toast.makeText(LoginActivity.this, "firstName : " + response.body().getUser().getFirstName() +
"lastName : " + response.body().getUser().getLastName(), Toast.LENGTH_SHORT).show();*/
}
else {
Toast.makeText(LoginActivity.this, "" + "User not exist", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(getApplicationContext(),"No Success", Toast.LENGTH_LONG).show();
try {
Log.i(TAG, "ERROR." + response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<CheckUser> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API." + t.getCause());
Toast.makeText(LoginActivity.this,"Unable to submit post to API."+t.getCause(),Toast.LENGTH_LONG).show();
}
});
}
谁能帮我解决一下谢谢^_^
这是API服务接口
public interface APIService {
@GET("node/read.php")
Call <NodeList> getNodes();
@GET("data/read.php")
//@FormUrlEncoded
Call<DataList> getDatas();
@POST("data/readByNode.php")
@FormUrlEncoded
Call<DataList> getDatasByNode(
@Field("node") String nodeName
);
//@POST("user/check.php")
@POST("user/check.php")
@FormUrlEncoded
Call<CheckUser> userLogIn(
@Field("username") String username,
@Field("password") String password
);
@POST("prediction/readDayPrediction.php")
@FormUrlEncoded
Call<Prediction> sendDifference(
@Field("difference") Integer difference
);
}
这是 RetrofitClient 类:
public class RetrofitClient {
private static Retrofit retrofit = null;
static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").setLenient().create();
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
<pre> try to change baseUrl www.servername.com/ don't append /apiV5 to baseurl but add it @Get("apiV5/user/check.php") fun check() </pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.