gpt4 book ai didi

android - 改造 POST : JSON object does't send to PHP server

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:48 25 4
gpt4 key购买 nike

我正在尝试通过 Android 的 POST 方法将 User 类的对象(使用 Gson-Retrofit)发送到服务器。如果服务器接收到任何数据,它会发送一个 JSON 对象给 App,其中包含 KEY“success”和“message”。但不幸的是,每次服务器都会向 App 发送 {"success":false,"message":"No"}

我认为从 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
$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);
}
?>

关于android - 改造 POST : JSON object does't send to PHP server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40933806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com