我想使用retrofit 2从我的api中获得string json,当我使用retrofit 1获得这个json时没有问题,但是使用retrofit 2为我返回null。
这是我的json的样子。
{"id":1,"Username":"admin","Level":"Administrator"}
这是我的API
@FormUrlEncoded
@POST("/api/level")
Call<ResponseBody> checkLevel(@Field("id") int id);
下面是我的代码的样子
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<ResponseBody> call = api.checkLevel(1);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
JsonObject post = new JsonObject().get(response.body().toString()).getAsJsonObject();
if (post.get("Level").getAsString().contains("Administrator")) {
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
我刚开始使用改进2,使用上面的代码,它总是使我的应用程序崩溃,因为response.body().toString()
返回空。
请指导我如何获得json字符串,以便我可以将其转换为JsonObject。
发布于 2016-12-05 19:57:15
使用此 将JSON转换为POJO,选择选项如下图所示
您将获得一个用于响应的POJO类,如下所示的
public class Result {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("Username")
@Expose
private String username;
@SerializedName("Level")
@Expose
private String level;
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The username
*/
public String getUsername() {
return username;
}
/**
*
* @param username
* The Username
*/
public void setUsername(String username) {
this.username = username;
}
/**
*
* @return
* The level
*/
public String getLevel() {
return level;
}
/**
*
* @param level
* The Level
*/
public void setLevel(String level) {
this.level = level;
}
}
并使用如下接口:
@FormUrlEncoded
@POST("/api/level")
Call<Result> checkLevel(@Field("id") int id);
并像这样调用:
Call<Result> call = api.checkLevel(1);
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
if(response.isSuccessful()){
response.body(); // have your all data
int id =response.body().getId();
String userName = response.body().getUsername();
String level = response.body().getLevel();
}else Toast.makeText(context,response.errorBody().string(),Toast.LENGTH_SHORT).show(); // this will tell you why your api doesnt work most of time
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(context,t.toString(),Toast.LENGTH_SHORT).show(); // ALL NETWORK ERROR HERE
}
});
并在Gradle中使用依赖项
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.+'
注意:该错误是因为您将JSON更改为POJO (通过在改进中使用addConverterFactory(GsonConverterFactory.create())
)。如果你想要JSON格式的响应,那么从Retrofit中删除addConverterFactory(GsonConverterFactory.create())
。如果不是,则使用上面的解决方案
发布于 2017-09-01 13:39:23
如果你想得到JSON格式的完整响应,试试这个:
我尝试了一种新方法,可以在不创建任何模型类的情况下,以JSON格式从服务器获取整个响应。我没有使用任何模型类从服务器获取数据,因为我不知道我会得到什么响应,或者它可能会根据需求而变化。
这是JSON响应:
{"contacts": [
{
"id": "c200",
"name": "sunil",
"email": "email@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
.
.
.
]}
API接口中的
公共接口登录{@POST(“/index.php/User/ ApiInterface”)//您的接口链接@FormUrlEncoded调用getmovies(@Field("user_email_address") String title,@Field("user_password") String body);}在您的主活动中调用
ApiInterface call apiService = ApiClient.getClient().create(ApiInterface.class);Call call= apiService.getmovies("a@gmail.com","123456");call.enqueue(call.enqueue(){ @Override public void onResponse(Call call,Response response) { Log.e("TAG","response 33:“+ApiInterface Gson().toJson(response.body( );} @Override public void onFailure(Call call,Throwable t) { Log.e("TAG","onFailure:"+t.toString() );//请求失败导致此处记录错误}});
获取参数
输出
发布于 2016-12-05 19:58:04
使用它来获取字符串
String res = response.body().string();
而不是
String res = response.body().toString();
并在将responsebody转换为字符串之前始终保持对null的检查
if(response.body() != null){
//do your stuff
}
https://stackoverflow.com/questions/40973633
复制相似问题