Retrofit POST: JSON object does#39;t send to PHP server(改造后:JSON对象不会发送到PHP服务器)
本文介绍了改造后:JSON对象不会发送到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过Android的POST方法将User类的对象(使用gson-Retrofit)发送到服务器。如果服务器接收到任何数据,它将向App发送一个JSON对象,其中包含Key&Quot;Success&Quot;和&Quot;Message&Quot;。但遗憾的是,每次服务器向App发送{";SUCCESS&QOT;:FALSE,&QOOT;MESSAGE&QOT;:&QOOT;NO&QOOT;}时。
我认为从MainActivity调用方法时有问题。为了更好地理解,下面给出了我的所有代码:
从MainActivity.java我调用此方法:
private void checkUserValidity(User userCredential){
ApiInterface apiInterface = RetrofitApiClient.getClient().create(ApiInterface.class);
Call<ValidityResponse> call = apiInterface.getUserValidity(userCredential);
call.enqueue(new Callback<ValidityResponse>() {
@Override
public void onResponse(Call<ValidityResponse> call, Response<ValidityResponse> response) {
ValidityResponse validity = response.body();
Toast.makeText(getApplicationContext(), validity.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
我的ApiInterface.java类是:
public interface ApiInterface {
@POST("/retrofit_login/login.php")
Call<ValidityResponse> getUserValidity(
@Body User userLoginCredential);
}
RetrofitApiClient.java类为:
public class RetrofitApiClient {
private static final String BASE_URL = "http://192.168.0.101"; //address of your localhost
private static Retrofit retrofit = null;
private static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
User.java类为:
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;
}
}
ValidityResponse.java类为:
public class ValidityResponse {
@SerializedName("success")
boolean successString;
@SerializedName("message")
String messageString;
public boolean isSuccess(){
return successString;
}
public String getMessage() {
return messageString;
}
}
最后服务器端php代码为:
<?php
if (isset($_POST['user_id']) && isset($_POST['password']))
$json = array('success' => true, 'message' => 'Yes');
else
$json = array('success' => false, 'message' => 'No');
echo json_encode($json);
?>
问题出在哪里?我现在该怎么办呢? 感谢您抽出时间。
php>推荐答案
问题出在您的PHP代码中。试试这个:
<?php
$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
//code to process data
if ($data == "" || empty($json_data['user_id']) || empty($json_data['password']))
{
$response = array('status' => false, 'message' => 'Invalid Values');
}
else
{
$response = array('status' => true,'message' => 'success');
}
echo json_encode($response);
}
?>
这篇关于改造后:JSON对象不会发送到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:改造后:JSON对象不会发送到PHP服务器
基础教程推荐
猜你喜欢
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
