我是 retrofit 新手。我完成了所有设置。 我在 build.gradle 文件中添加了这个 gradle
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
我的界面是这样的:
public interface ILoginInterface {
String BASE_URL= "MY_BASE_URL/";
@POST("MY/API")
Call<LoginResponseEntity> startLogin(@Body JSONObject jsonObject);
class Factory{
private static ILoginInterface instance;
public static ILoginInterface getInstance(){
if(instance==null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
instance = retrofit.create(ILoginInterface.class);
}
return instance;
}
}
}
我的调用过程是这样的:
ILoginInterface.Factory.getInstance().startLogin(jsonObject).enqueue(new Callback<LoginResponseEntity>() {
@Override
public void onResponse(Call<LoginResponseEntity> call, retrofit2.Response<LoginResponseEntity> response) {
Log.d("MS",response.body().fullName);
}
@Override
public void onFailure(Call<LoginResponseEntity> call, Throwable t) {
Log.d("MS",t.getMessage());
}
});
这里的jsonObject
是这样的:
{"user_name":"sajedul Karim", "password":"123456"}
这里似乎一切正常,但我没有得到正确的回应。 我找到了解决办法。它是 here 。有没有人有合适的解决方案,比如 Volley JsonObjectRequest
最佳答案
可能是你在导入
import org.json.JSONObject;
你应该使用
import com.google.gson.JsonObject;
然后你会得到它的值(value)。
关于android - 使用 Retrofit2 的 Json 对象请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37638716/