是否可以在retrofit2中处理json响应?

时间:2017-03-12 09:24:39

标签: android json retrofit2

1)Json回应。

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : 1
Current fruit : 45

2)使用http://www.jsonschema2pojo.org/创建java类。

3)

  {
"customer_info": {
    "customer_id": "1",
    "customer_group_id": "1",
    "store_id": "0",
    "firstname": "john",
    "lastname": "kam",
    "email": "johnoconner@gmail.com",
    "telephone": "+3458690730867",
    "fax": "",
    "password": "c5bf3e452f02e3c639ce8513245d500cfdfbad9c",
    "salt": "qXXoVmh0j",
    "cart": null,
    "wishlist": null,
    "newsletter": "0",
    "address_id": "1",
    "custom_field": "",
    "ip": "45.113.251.60",
    "status": "1",
    "approved": "1",
    "verified": "0",
    "safe": "0",
    "token": "",
    "date_added": "2017-03-08 15:49:28"
},
"error_warning": "",
"redirect": "",
"success": "",
"email": "johnoconner@gmail.com",
"password": "12345678"
}

4)我得到的错误是

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1行路径为STRING

5)抓住很多东西,但没有理解错误是什么。

6)我也附上了我的java课程。

2 个答案:

答案 0 :(得分:0)

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1行路径为STRING

请确保从服务器收到的输出响应正常。检查双引号。

答案 1 :(得分:0)

您的基本网址必须是 http://thefruitbowl.in/ 和您的终端? route = api / web_api / login

改装电话

public void retrofitTest()
{
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://thefruitbowl.in/") // not the base url
            .addConverterFactory(GsonConverterFactory.create(gson));

    Retrofit retrofit = builder.build();

    LoginInterface loginInterface=retrofit.create(LoginInterface.class);
    Call<Example> exampleCall = loginInterface.checkUserExistOrNot("abc@gmail.com", "12345");
    exampleCall.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, retrofit2.Response<Example> response) {
            Example example = response.body();
            Log.i("response",example.getEmail());
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });
}

界面

public interface LoginInterface
{

    @GET("?route=api/web_api/login")
    @Headers("Content-Type: application/json")
    Call<Example> checkUserExistOrNot(@Query("email") String email, @Query("password") String password);
}

我确实得到了回复

 response: abc@gmail.com

对于评论的网址pojos

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CategoryResponse {

@SerializedName("success")
@Expose
private int success;
@SerializedName("categories")
@Expose
private List<Category> categories = null;

public int getSuccess() {
    return success;
}

public void setSuccess(int success) {
    this.success = success;
}

public List<Category> getCategories() {
    return categories;
}

public void setCategories(List<Category> categories) {
    this.categories = categories;
}

}

然后

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Category {

@SerializedName("category_id")
@Expose
private String categoryId;
@SerializedName("parent_id")
@Expose
private String parentId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("image")
@Expose
private Boolean image;
@SerializedName("href")
@Expose
private String href;
@SerializedName("categories")
@Expose
private Object categories;

public String getCategoryId() {
return categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public String getParentId() {
return parentId;
}

public void setParentId(String parentId) {
this.parentId = parentId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Boolean getImage() {
return image;
}

public void setImage(Boolean image) {
this.image = image;
}

public String getHref() {
return href;
}

public void setHref(String href) {
this.href = href;
}

public Object getCategories() {
return categories;
}

public void setCategories(Object categories) {
this.categories = categories;
}

}

您的界面

public interface LoginInterface
    {

        @GET("?route=api/web_api/login")
        @Headers("Content-Type: application/json")
        Call<Example> checkUserExistOrNot(@Query("email") String email, @Query("password") String password);

        @GET("?route=api/web_api/categories")
        @Headers("Content-Type: application/json")
        Call<CategoryResponse> get()

    }

您的电话

public void retrofitTest2()
{
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://thefruitbowl.in/")
            .addConverterFactory(GsonConverterFactory.create(gson));

    Retrofit retrofit = builder.build();

    LoginInterface loginInterface=retrofit.create(LoginInterface.class);
    Call<CategoryResponse> categoryResponseCall = loginInterface.get();

    categoryResponseCall.enqueue(new Callback<CategoryResponse>() {
        @Override
        public void onResponse(Call<CategoryResponse> call, retrofit2.Response<CategoryResponse> response) {
            CategoryResponse categoryResponse = response.body();
            if(categoryResponse!=null)
            {
                Log.i("response",categoryResponse.getCategories().get(0).getName());
            }
        }

        @Override
        public void onFailure(Call<CategoryResponse> call, Throwable t) {

        }
    });


}