使用改造

时间:2017-06-06 17:27:34

标签: java android retrofit2

我正在尝试使用php程序验证来自我的服务器的登录请求。我正在使用改造作为客户端api。作为服务器响应,我将一个字符串消息和布尔类型成功状态返回到我的Android应用程序。每当我尝试访问服务器响应时,我的应用程序崩溃。

这是我尝试过的:

我的登录界面:

public interface ApiInterface {

@POST("retrofit_get_post/server_side_code.php")
Call<ServerResponse> getUserValidity(@Body User userLoginCredential);

}

用户信息:

public class User {

@SerializedName("user_id")
private String userId;
@SerializedName("password")
private String password;

public User(){}

public void setUserId(String userId) {
    this.userId = userId;
}

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

}

ServerResponse:

public class ServerResponse {

@SerializedName("status")
boolean statusString;
@SerializedName("message")
String messageString;

public boolean isSuccess(){
    return statusString;
}

public String getMessage() {
    return messageString;
}

}

主要活动

public class MainActivity extends AppCompatActivity {

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

    User user = new User();
     user.setUserId("bishal");
    user.setPassword("123");

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://baseurl/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    ApiInterface apiInterface = retrofit.create(ApiInterface.class);
    Call<ServerResponse> call = apiInterface.getUserValidity(user);

    call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {

            ServerResponse validity = response.body();
             //String output= validity.getMessage().toString();
            if(validity.isSuccess())  //***after executing this statement my app crashes****
            Log.i("Tag :" ,"Okkkk");
            Toast.makeText(getApplicationContext(),"success!!!",Toast.LENGTH_LONG).show();
        }

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

            Toast.makeText(getApplicationContext(),"Error!!!",Toast.LENGTH_LONG).show();
        }
    });

}
}

服务器端PHP代码

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{
    $data = file_get_contents('php://input');
    $json_data = json_decode($data , true);

    //code to process data
    if ($data == "" || empty($json_data['user_id']) || empty($json_data['password']))
    {
        $response = array('status' => false, 'message' => 'Invalid Values');    
    }
    else
    {
        if($json_data['user_id']=='bishal' && $json_data['password']=='123')
            $response = array('status' => true, 'message' => 'Wow! You are a valid user!');
        else
            $response = array('status' => false, 'message' => 'User ID or password is not valid');
    }

    echo json_encode($response);
}

?>

这是logcat中的错误报告:

java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.String okhttp3.ResponseBody.string()&#39;在null对象引用上                                                                         at me.imbishal.loginapp.MainActivity $ 1.onResponse(MainActivity.java:43)

0 个答案:

没有答案