1

I'm using Retrofit 2 to get some data from PHP API. The server receives the call, however, $_POST is empty!

I tried sending data using various ways, but still no luck!

here is sample php api

<?php

$token = $_POST["api_token"];

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

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

I tried calling api using postman (working fine)

enter image description here

here is my Client interface 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);
}

Also, this is response class 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;
    }
}

and my request function

    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;
}

Unfortunately, response.body().getApiToken() returns null!

by the way, I tried other solutions like, . https://stackoverflow.com/a/37831830/5193899

EDIT,

My problem is with the android app, as the API works fine with iOS app and API testers, this solution doesn’t solve my problem as it requires a change to API code!

Receive JSON POST with PHP

Joseph Ali
  • 345
  • 1
  • 5
  • 11
  • Does this answer your question? [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – CBroe Mar 31 '21 at 07:09
  • unfortunately no. My problem is with the android app, as the API works fine with iOS app and API testers, this solution may solve my problem, but it requires a change to API code! – Joseph Ali Mar 31 '21 at 08:19

1 Answers1

3

In your code you have the content type set to application/json

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

According to this $_POST only returns

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

In order to read the token from PHP you have 2 options.

You can either

  • Remove the header annotation at postParams

  • Use this code to read application/json in PHP.

$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
$token = $json_data['api_token'];
Tiago Oliveira
  • 1,582
  • 1
  • 16
  • 31