0

Всем доброго времени суток! Я пытаюсь разобраться в библиотеке Retrofit2. При выполнении POST запроса из приложения, данные не отправляются на сервер (или отправляются пустыми). Но ответ от сервера приходит (json). Я пробовала отправлять с помощью различных аннотаций (@Field, @Body с Object, @Body с HashMap), но каждый раз сервер не получал данные. Вот мой код:

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.test.retrofitpostdatagetjson"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation 'com.google.code.gson:gson:2.8.4'

    implementation 'com.squareup.retrofit2:retrofit:2.5.0'

    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

APIClient.java

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class APIClient {
    private static final String BASE_URL = "https://myurl.ru/";
    private static Retrofit retrofit;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

ApiUtils.java

public class ApiUtils {

    private ApiUtils() {}

    public static APIInterface getAPIService() {

        return APIClient.getClient().create(APIInterface.class);
    }
}

APIInterface.java

import com.test.retrofitpostdatagetjson.model.DataResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface APIInterface {
    @POST("test_json_with_post")
    Call<DataResponse> createData(@Body DataResponse data);

}

DataResponse.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;

public class DataResponse implements Serializable {

    @SerializedName("numb")
    @Expose
    private int numb;
    @SerializedName("name")
    @Expose
    private String name;

    @SerializedName("emp")
    @Expose
    public String value;
    @SerializedName("status")
    @Expose
    public String status;

    public DataResponse(int numb, String name) {
        this.numb = numb;
        this.name = name;
    }

    public int getNumb() {
        return numb;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public String getStatus() {
        return status;
    }

    public void setNumb(int numb) {
        this.numb = numb;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "DataResponse{" +
                "numb=" + numb +
                ", name='" + name + '\'' +
                ", value='" + value + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.test.retrofitpostdatagetjson.api.APIInterface;
import com.test.retrofitpostdatagetjson.model.DataResponse;
import static com.test.retrofitpostdatagetjson.api.ApiUtils.getAPIService;

public class MainActivity extends AppCompatActivity {

    APIInterface apiInterface;
    int numb = 0;
    String name = "test row";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apiInterface = getAPIService();

        if (CommonMethod.isNetworkAvailable(MainActivity.this))
            sendPost(numb, name);
        else
            CommonMethod.showAlert("Internet Connectivity Failure", MainActivity.this);

    }

    private void sendPost(int numb, String name) {

        final DataResponse data = new DataResponse(numb, name);
        Log.w("retroTest", "sent    -->  " + data.toString());
        Call<DataResponse> call1 = apiInterface.createData(data);
        call1.enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                DataResponse dataResponse = response.body();
                if (dataResponse != null) {
                    Log.w("retroTest", "received    -->  " + dataResponse.toString());
                }
            }

            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show();
                call.cancel();
            }
        });
    }

}

Мои логи:

W/retroTest: sent  -->  DataResponse{numb=0, name='test row', value='null', status='null'}
W/retroTest: received  -->  DataResponse{numb=0, name='null', value='null', status='2'}

PHP file на сервере:

<?php
header("Content-type: application/json; charset=utf-8");

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);

$numb = $input['numb'];
$name = $input['name'];

if (isset($_POST['numb']) && !empty($_POST['numb'])) {   
  $numb = $_POST['numb']; }
if (isset($_POST['name']) && !empty($_POST['name'])) {   
  $name = $_POST['name']; }

if (!$numb) {
$json = json_encode( array(
"emp" => $name,
"status" => "2"));
} else {
$json = json_encode( array(
"emp" => "nothing",
"status" => "2"));
}

$myfile = fopen("testfile.txt", "w");
fwrite($myfile, $inputJSON);
fwrite($myfile, "\n");
fwrite($myfile, $numb);
fwrite($myfile, "\n");
fwrite($myfile, $name);
fclose($myfile);


echo $json;
?>

Файл testfile.txt также всегда пуст, Но когда я отправляю POST с помощью Postman всё работает, и данные записываются в файл testfile.txt. enter image description here enter image description here

1 ответ 1

3

Я выяснила причину моей проблемы .. Это так просто ... Всё дело в слеше в конце URL. Я использую index.php, поэтому, когда я отправляю POST на мой URL-адрес, я должна поставить в конце слеш.

Вместо этого:

 @POST ( "test_json_with_post")

Я должна написать это:

 @POST ( "test_json_with_post/")

в моем APIInterface.java. И все работает!

Надеюсь, это кому-нибудь когда-нибудь поможет)

1
  • 1
    Спасибо, Мария. Очень помогло Ваше решение
    – Антон
    21 апр 2020 в 18:16

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.