【问题标题】:Retrofit 2 Server not Receiving POST Android改造 2 服务器不接收 POST Android
【发布时间】:2021-06-26 21:48:51
【问题描述】:

我正在使用 Retrofit 2 从 PHP API 获取一些数据。服务器收到调用,但是$_POST是空的!

我尝试了各种方式发送数据,但仍然没有成功!

这里是示例 php api

<?php

$token = $_POST["api_token"];

$myObj = new stdClass();
$myObj -> api_token = $token;

$jsonObj = json_encode($myObj);
echo $jsonObj;
?>

我尝试使用邮递员调用 api(工作正常)

这是我的客户端界面ClientTester

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface ClientTester {
    @Headers({"Content-Type: application/json;charset=UTF-8"})
    @POST("/****/****.php")
    Call<ResponseTester> postParams(
             @Body Map<String, String> requestBody);
}

另外,这是响应类ResponseTester

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ResponseTester {
    @Expose
    @SerializedName("api_token")
    private String apiToken;

    public String getApiToken() {
        return apiToken;
    }

    public void setApiToken(String apiToken) {
        this.apiToken = apiToken;
    }
}

还有我的请求函数

    private void sendRequest() {

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://****.com")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    ClientTester ClientTester = retrofit.create(ClientTester.class);
    Call<ResponseTester> call = ClientTester.postParams(getBodyParams());

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

            Log.d("VolleyResponse", "onResponse: " + response.body().getApiToken());
            tvResponse.setText(response.body().getApiToken()+"...");

        }

        @Override
        public void onFailure(Call<ResponseTester> call, Throwable t) {
            Log.d("VolleyResponse", "onFailure: " + t.toString());
            tvResponse.setText(t.toString());

        }
    });


private Map<String, String> getBodyParams() {
    Map<String, String> params = new HashMap<String, String>();
    params.put("api_token", "SomeToken");
    return params;
}

很遗憾,response.body().getApiToken() 返回 null!

顺便说一句,我尝试了其他解决方案,例如, . https://stackoverflow.com/a/37831830/5193899

编辑,

我的问题在于 android 应用程序,因为 API 可以很好地与 iOS 应用程序和 API 测试人员配合使用,这个解决方案并不能解决我的问题,因为它需要更改 API 代码!

Receive JSON POST with PHP

【问题讨论】:

  • 这能回答你的问题吗? Receive JSON POST with PHP
  • 不幸的是没有。我的问题是 android 应用程序,因为 API 与 iOS 应用程序和 API 测试人员可以正常工作,这个解决方案可能会解决我的问题,但它需要更改 API 代码!

标签: php android api retrofit2


【解决方案1】:

在您的代码中,您将内容类型设置为application/json

@Headers({"Content-Type: application/json;charset=UTF-8"})
    @POST("/****/****.php")
    Call<ResponseTester> postParams(
             @Body Map<String, String> requestBody);

根据this$_POST只返回

  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain

为了从 PHP 中读取令牌,您有 2 个选项。

你可以

  • 去掉postParams处的头部注解

  • 使用此代码在 PHP 中读取 application/json

$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
$token = $json_data['api_token'];

【讨论】:

  • $_JSON?我想你的意思是$_POST 那里。
  • 我刚刚尝试了你的解决方案,它工作得非常好,我唯一担心的是我没有制作 API,这只是一个示例,有没有办法解决这个问题表格android代码本身?
  • @CBroe 是的,你是对的,这是一个错字,我将编辑我的 awnser
  • @Joseph Ali 如果您删除 ClientTester 中的 @Headers({...}) 注释,它应该可以在不更改 API 的情况下工作
  • @Joseph Ali 我编辑了我的遮阳篷以更好地解释它
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多