skip to Main Content

I have a problem about getting value from php file and showing it as a Toast message.

I get result value as “null”.

How can I fix it?

Here is my php code.

...
$x = (array('Result' => "Successful"));
echo json_encode($x,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
...

android code

x.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                if(response.isSuccessful()){
                    Result result = response.body();
                    String message = result.getResult();
                    Log.i(LOG,"gonder() | onResponse : " + message);
                    Toast.makeText(getApplicationContext(),
                            message, Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });

2

Answers


  1. Chosen as BEST ANSWER

    My answer,

    I changed the variable as "result" instead of defing "Result" and I fix the issue.

    $x = (array('result' => "Successful"));
    echo json_encode($x,JSON_UNESCAPED_UNICODE);
    

  2. In php use the json_encode

    $x = (array('Result' => "Successful"));
    echo json_encode($x,JSON_UNESCAPED_UNICODE);
    

    And In android before showing the response message as as a Toast message you can check you are getting the Successful message or not.

    Like.

    call.enqueue(new Callback<Result>() {
        @Override            
        public void onResponse(Call<Result> call, Response<Result> response) {
            if(response.body().getResult().equals("Successful")){
              // add your code here to showing messawge as a Toast.
            }
            else {
              // display error
            }
        }
        @Override            
        public void onFailure(Call<Result> call, Throwable t) {//display errror message
    
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search