改造:出现错误**第 1 行第 1 列的输入结束**

Retrofit : Getting error ** End of input at line 1 column 1**

我不知道我的问题出在哪里。我必须 post 原始 body 喜欢

{"app_id":"abced","app_key":"abced","request":"login","username":"abce@gmail.com","password":"1234"}

我正在使用改造 2.0。我创建了一个类似

的界面
    @FormUrlEncoded
@POST("index.php/")
Call<LoginResponse> getHome(@Field("app_id") String request,
                            @Field("app_key") String request1,
                            @Field("request") String request2,
                            @Field("username") String request3,
                            @Field("password") String request4);

并这样调用:

 Retrofit retrofitget = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MyAPI ApiData = retrofitget.create(MyAPI.class);
        Call<LoginResponse> GalleryGetSet = ApiData.getHome("abced","abced","login","abce@gmail.com","1234");
        GalleryGetSet.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                Log.e(TAG, "data1" + response.body().getError());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.e(TAG, "Error2" + t.getMessage());
            }
        });

总是出现错误:第 1 行第 1 列的输入结束 通过改造 post 对原始 body 数据进行 post 处理是正确的还是我错了。已经 google 它但没有得到我的解决方案。 请任何人帮助我。

我也试过这种方式 界面中

@FormUrlEncoded
@POST("index.php")
Call<LoginResponse> getHome1(@Field("login") String data);

并在调用时

 Retrofit retrofitget = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        MyAPI ApiData = retrofitget.create(MyAPI.class);

        Call<LoginResponse> listGalleryGetSet = ApiData.getHome("ABCD","ABCD","login","ABC@gmail.com","1234");
        listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                Log.e(TAG, "data1" + response.body().getError());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.e(TAG, "Error" + t.getMessage());
            }
        });

我的登录响应class

public class LoginResponse {

private String error;

private Boolean vlink;

/**
 *
 * @return
 * The error
 */
public String getError() {
    return error;
}

/**
 *
 * @param error
 * The error
 */
public void setError(String error) {
    this.error = error;
}

/**
 *
 * @return
 * The vlink
 */
public Boolean getVlink() {
    return vlink;
}

/**
 *
 * @param vlink
 * The vlink
 */
public void setVlink(Boolean vlink) {
    this.vlink = vlink;
}

}

依赖关系

 compile 'com.squareup.retrofit2:retrofit-converters:2.0.0-beta3'
 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
 compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

提前致谢

将您的示例正文 Json 粘贴到此站点,select 来源 Json,注释 NoneJson Schema 2 Pojo

然后在你的 Api 中:

@POST("index.php")
public Call<LoginResponse> getHome(@Body YourBodyPojo pojo);

然后调用:

OkHttpClient okClient = new OkHttpClient.Builder()
            .connectTimeout(5,TimeUnit.SECONDS)
            .build();


    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    Retrofit client = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(okClient)
            .build();

    MyAPI apiData = retrofitget.create(MyAPI.class);

    LoginRequest loginRequest=new LoginRequest(); 
    loginRequest.setUserName("abc"); 
    loginRequest.setPassword("123");  
    Call<LoginResponse> listGalleryGetSet = apiData.getHome(loginRequest);
    listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            Log.e(TAG, "data1" + response.body().getError());
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Log.e(TAG, "Error" + t.getMessage());
        }
    });

回应class:

package com.example;

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


public class YourResponse{

@SerializedName("error")
@Expose
private String error;
@SerializedName("vlink")
@Expose
private Boolean vlink;

/**
 * 
 * @return
 * The error
 */
 public String getError() {
     return error;
 }

/**
 * 
 * @param error
 * The error
 */
 public void setError(String error) {
    this.error = error;
 }

/**
 * 
 * @return
 * The vlink
 */
 public Boolean getVlink() {
    return vlink;
 }

/**
 * 
 * @param vlink
 * The vlink
 */
 public void setVlink(Boolean vlink) {
     this.vlink = vlink;
 }

}

正文class:

package com.example;


public class YourBodyClass{

private String appId;
private String appKey;
private String request;
private String username;
private String password;

/**
 * 
 * @return
 * The appId
 */
public String getAppId() {
   return appId;
}

/**
 * 
 * @param appId
 * The app_id
 */
public void setAppId(String appId) {
   this.appId = appId;
}

/**
 * 
 * @return
 * The appKey
 */
public String getAppKey() {
   return appKey;
}

/**
 * 
 * @param appKey
 * The app_key
 */
public void setAppKey(String appKey) {
   this.appKey = appKey;
}

/**
 * 
 * @return
 * The request
 */
public String getRequest() {
   return request;
}

/**
 * 
 * @param request
 * The request
 */
public void setRequest(String request) {
   this.request = request;
}

/**
 * 
 * @return
 * The username
 */
public String getUsername() {
   return username;
}

/**
 * 
 * @param username
 * The username
 */
public void setUsername(String username) {
   this.username = username;
}

/**
 * 
 * @return
 * The password
 */
public String getPassword() {
   return password;
}

/**
 * 
 * @param password
 * The password
 */
public void setPassword(String password) {
   this.password = password;
}

}

而不是使用

@POST("index.php")

请尝试使用

@POST("/index.php")

和 post 作为

Call<LoginResponse> getHome(@Body YourBodyPojo pojo);

这一定会对你有所帮助。

只是重新发布一个对我有用的答案。

@Override
public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
        if (response.isSuccessful()) {

        //Do something if response is ok
        } else {

            JsonParser parser = new JsonParser();
            JsonElement mJson = null;
            try {
                mJson = parser.parse(response.errorBody().string());
                Gson gson = new Gson();
                MyError errorResponse = gson.fromJson(mJson, MyError.class);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }