【问题标题】:there is connection but why i don't get the response retrofit有连接但为什么我没有得到响应改造
【发布时间】:2016-11-29 05:56:21
【问题描述】:

嘿,我有 php 服务器,那里有用户名:jonik 密码 123456 我在 android 页面上创建用于登录我正在测试这 2 件事我得到响应但我不知道日志中的哪个位置有我的成功响应我想把它放在 mainactivity 中所以也许我可以做如果 statment 去下一个活动,我有这个错误((((我在与服务器com.google.gson.JsonSyntaxException连接后收到此错误:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第1列路径$) ))

主活动

   private Communicator communicator;
private String username, password;
private EditText usernameET, passwordET;
private Button loginButtonPost, loginButtonGet;
private TextView information, extraInformation;
private final static String TAG = "MainActivity";
public static Bus bus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    communicator = new Communicator();

    usernameET = (EditText)findViewById(R.id.usernameInput);
    passwordET = (EditText)findViewById(R.id.passwordInput);
    //This is used to hide the password's EditText characters. So we can avoid the different hint font.
    passwordET.setTransformationMethod(new PasswordTransformationMethod());

    loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
    loginButtonPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            username = usernameET.getText().toString();
            password = passwordET.getText().toString();
            usePost(username, password);
        }
    });



    information = (TextView)findViewById(R.id.information);
    extraInformation = (TextView)findViewById(R.id.extraInformation);
}

private void usePost(String username, String password){
    communicator.loginPost(username, password);
}

服务器响应

 public class ServerResponse implements Serializable {
    @SerializedName("returned_username")
    private String username;
    @SerializedName("returned_password")
    private String password;
    @SerializedName("message")
    private String message;
    @SerializedName("response_code")
    private int responseCode;

    public ServerResponse(String username, String password, String message, int responseCode){
        this.username = username;
        this.password = password;
        this.message = message;
        this.responseCode = responseCode;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(int responseCode) {
        this.responseCode = responseCode;
    }
}

服务器事件

public class ServerEvent {
private ServerResponse serverResponse;

public ServerEvent(ServerResponse serverResponse) {
    this.serverResponse = serverResponse;
}

public ServerResponse getServerResponse() {
    return serverResponse;
}

public void setServerResponse(ServerResponse serverResponse) {
    this.serverResponse = serverResponse;
}

界面

public interface Interface {
@FormUrlEncoded

@POST("/cult_tickets/request.php")
void postData(
              @Field("username") String username,
              @Field("password") String password,
              Callback<ServerResponse> serverResponseCallback);

通讯器

 public class Communicator {
    private static  final String TAG = "Communicator";
    private static final String SERVER_URL = "http://192.168.3.105";
    public void loginPost(String username, String password){
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(SERVER_URL)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();
        Interface communicatorInterface = restAdapter.create(Interface.class);
        Callback<ServerResponse> callback = new Callback<ServerResponse>() {
            @Override
            public void success(ServerResponse serverResponse, Response response2) {
                if(serverResponse.getResponseCode() == 0){
                    BusProvider.getInstance().post(produceServerEvent(serverResponse));
                }else{
                    BusProvider.getInstance().post(produceErrorEvent(serverResponse.getResponseCode(), serverResponse.getMessage()));
                }

            }

            @Override
            public void failure(RetrofitError error) {
                if(error != null ){
                    Log.e(TAG, error.getMessage());
                    error.printStackTrace();
                }
                BusProvider.getInstance().post(produceErrorEvent(-200,error.getMessage()));
            }
        };
        communicatorInterface.postData(username, password, callback);
    }

@Produce
public ServerEvent produceServerEvent(ServerResponse serverResponse) {
    return new ServerEvent(serverResponse);
}

@Produce
public ErrorEvent produceErrorEvent(int errorCode, String errorMsg) {
    return new ErrorEvent(errorCode, errorMsg);
}
}

最后是总线提供者

public class BusProvider {

private static final Bus BUS = new Bus();

public static Bus getInstance(){
    return BUS;
}

public BusProvider(){}

}

【问题讨论】:

  • 您的服务器正在给出字符串响应
  • 是成功字或错误,如果用户名和密码错误
  • 作为响应,您期望 ServerResponse 对象,即 jsonobject。但响应只是一个字符串,要么将服务器响应更改为发送 jsonobject,要么将回调更改为仅接收字符串。
  • 我将所有内容都更改为字符串仍然相同的错误,请你告诉我我该怎么做 If(..??>>? == "success") 例如意图我应该在哪里写这个如何得到回应

标签: android retrofit


【解决方案1】:

即使没有看到您的 JSON 字符串,您也可以从错误消息中看出它不是被解析为您的类实例的正确结构。

Gson 期望您的 JSON 字符串以对象左大括号开头。例如

{

但是你传递给它的字符串以一个开引号开始

"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多