0

Hey I have php server in there there is username : jonik password 123456 i created on android page for login i'm testing this 2 things im getting response but i dont know where just in the log there is my success response i want to put it in the mainactivity so maybe i can do if statment to go to next activity and i have this error ((((i get this error after the connection with the server com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $)))

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

interface

public interface Interface {
@FormUrlEncoded

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

Communicator

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

finally busprovider

public class BusProvider {

private static final Bus BUS = new Bus();

public static Bus getInstance(){
    return BUS;
}

public BusProvider(){}

}

EasyE
  • 44
  • 9
  • 1
    your server is giving string response – Pr38y Nov 29 '16 at 06:14
  • yes success word or error if the username and the pass is wrong – EasyE Nov 29 '16 at 06:57
  • in response you're expecting `ServerResponse` object, that is a jsonobject. but the response is just a string, either change your server response to send jsonobject, or change your callback to receive String only. – Pr38y Nov 29 '16 at 07:37
  • i changed everything to string still same error and please can u tell me how can i If(..??>>? == "success") for example intent where should i wrote this how to get the response – EasyE Nov 29 '16 at 07:49
  • check http://stackoverflow.com/a/23854605/797534 – Pr38y Nov 29 '16 at 08:47

1 Answers1

-1

Even without seeing your JSON string you can tell from the error message that it is not the correct structure to be parsed into an instance of your class.

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{

But the string you have passed to it starts with an open quotes

"

Amit Joshi
  • 36
  • 4