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服务器


猜你喜欢
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 拆分 Drawable 2022-01-01