有联系,但为什么我没有得到响应改造

时间:2016-11-29 05:56:21

标签: android retrofit

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

MainActivity

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

ServerResponse

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

serverEvent

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

终于busprovider

public class BusProvider {

private static final Bus BUS = new Bus();

public static Bus getInstance(){
    return BUS;
}

public BusProvider(){}

}

1 个答案:

答案 0 :(得分:-1)

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

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

{

但是您传递给它的字符串以引号引起来