改造:获取错误**第1行第1列的输入结束**

时间:2016-05-18 15:25:47

标签: android post retrofit retrofit2

我不知道我的问题在哪里。我要发布像

这样的原始身体
{"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列的输入结束 通过改造后发布原始数据或者我错了是正确的过程吗?已经谷歌但没有得到我的解决方案。 请有人帮帮我。

我也试过这种方式 界面

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

我的LoginResponse类

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

}

**This is how I'm posting on postman**

依赖

 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'

提前致谢

3 个答案:

答案 0 :(得分:4)

而不是使用

@POST("index.php")

请尝试使用

@POST("/index.php")

并以

发布
Call<LoginResponse> getHome(@Body YourBodyPojo pojo);
肯定这会对你有帮助。

答案 1 :(得分:1)

将示例正文Json粘贴到此网站,选择来源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());
        }
    });

响应类:

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

}

身体类:

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

}

答案 2 :(得分:0)

只需重新发布对我有用的答案。

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

        }