Json 使用 Retrofit2 的对象请求
Json Object Request using Retrofit2
我是改装新手。我完成了所有设置。
我在 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;
那么你就会得到它的价值。
我是改装新手。我完成了所有设置。 我在 build.gradle 文件
中添加这个 gradlecompile '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;
那么你就会得到它的价值。