Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否允许在第二个版本中处理json响应?_Android_Json_Retrofit2 - Fatal编程技术网

Android 是否允许在第二个版本中处理json响应?

Android 是否允许在第二个版本中处理json响应?,android,json,retrofit2,Android,Json,Retrofit2,1) Json响应 { "customer_info": { "customer_id": "1", "customer_group_id": "1", "store_id": "0", "firstname": "john", "lastname": "kam", "email": "johnoconner@gmail.com", "telephone": "+3458690730867", "fax": "", "p

1) Json响应

  {
"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"
}
2) 用于生成java类

(三)

Gson Gson=new GsonBuilder()
.setLenient()
.create();
reformation.Builder=新的reformation.Builder()
.baseUrl(“http://.in/?route=api/web_api/")
.addConverterFactory(GsonConverterFactory.create(gson));
改装改装=builder.build();
LoginInterface LoginInterface=改装.create(LoginInterface.class)
Call Call=loginInterface.checkUserExistOrNot(“abc@gmail.com", "12345");
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.i(“response is”,response.body().toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
4) 我得到的错误是

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$

5) 抓很多东西,但不知道是哪里出了问题

6) 我还附加了我的java类

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$


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

您的基本url必须是,并且您的端点是路由=api/web\u 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) {

        }
    });
}
对于已注释的url,请单击POJO

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;
}
你的界面

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);
}
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()

    }
公共接口登录接口
{
@GET(“?route=api/web\u api/login”)
@标题(“内容类型:application/json”)
调用checkUserExistOrNot(@Query(“email”)字符串email,@Query(“password”)字符串password);
@获取(“?路由=api/web\U api/类别”)
@标题(“内容类型:application/json”)
调用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) {

        }
    });


}
public-test2()
{
Gson Gson=new GsonBuilder()
.setLenient()
.create();
reformation.Builder=新的reformation.Builder()
.baseUrl(“http://thefruitbowl.in/")
.addConverterFactory(GsonConverterFactory.create(gson));
改装改装=builder.build();
LoginInterface LoginInterface=改装.create(LoginInterface.class);
调用categoryResponseCall=loginInterface.get();
categoryResponseCall.enqueue(新的回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
CategoryResponse CategoryResponse=response.body();
if(categoryResponse!=null)
{
Log.i(“response”,categoryResponse.getCategories().get(0.getName());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}

Log.i(“response is”,response.body().toString())中的lo
这并不例外。如果根本看不到日志,则必须看到带有异常的stacktrace
t.printStackTrace()它给出了一个com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_对象,但在第1行第1列路径处是字符串您的响应是一个字符串,它应该是一个json对象。您需要检查您的响应“再进行一次调用”是什么意思?基本URL将相同,但第二次调用没有查询参数,但该类别再次出现相同错误。我发现接口中存在问题。只有端点更改。在与端点的接口中还需要一个方法吗?route=api/web\u api/categoriesya我添加了该方法,但它再次给我java.lang.IllegalStateException:应为BEGIN\u数组,但在第1行第2列为BEGIN\u对象,可能我缺少什么。
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) {

        }
    });


}