C
C
Chesterfield252020-10-08 11:48:47
Java
Chesterfield25, 2020-10-08 11:48:47

How to send Post request in json using retrofit2?

Help with retrofit POST request. No way to study! When using the example below and clicking on the button in the application, it gives an error:

E/MainActivity: onResponse: null

5f7ecf8aafca4074265893.png

I got the essence of a partial error:
I have two classes, one in which the data for sending ApiClients, the other in which the data for receiving Todo. There are two buttons in activity_main, one for receiving data and the other for sending. But in order to get Todo data, I need to send ApiClients first. Simply put, these two classes do not interact with each other. Tell me how to put this not up to code together so that it would be workable?

The essence of the task: sending the user a certain amount to the balance at the click of a button!
All API documentation I work with https://faucetpay.io/page/api-documentation
In the Todo class, the data to receive in ApiClients is the data to send.

Sending address: https://faucetpay.io/api/v1/send

Sending data example:

КЛЮЧ: api_key=74445766sd9c0ebe19ca067471be1b41be398f5c
СУМА: amount=50
КОШЕЛЁК: to=MQYSDrNwiJxr1VmLiJHNqaMtJeNon5NUGn
ВАЛЮТА: currency=LTC


Sample response to a request:

{
    "status": 200,
    "message": "OK",
    "rate_limit_remaining": 9.99780024,
    "currency": "BTC",
    "balance": "8673047351",
    "balance_bitcoin": "86.73047351",
    "payout_id": 109,
    "payout_user_hash": "c448e31098a8dfb48248f7e2374e77674bb90925"
}


Application structure:

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GET TODOS"
        android:onClick="getTodos"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POST TODOS"
        android:onClick="postTodos"/>

</LinearLayout>


MainActivity class
ublic class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    ApiInterface apiInterface;

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

        apiInterface = ApiClient.getClient().create(ApiInterface.class);

    }

    public void getTodos(View view) {

        Call<List<Todo>> call = apiInterface.getTodos();
        call.enqueue(new Callback<List<Todo>>() {
            @Override
            public void onResponse(Call<List<Todo>> call, Response<List<Todo>> response) {
                Log.e(TAG, "onResponse: " + response.body());
            }

            @Override
            public void onFailure(Call<List<Todo>> call, Throwable t) {
                Log.e(TAG, "onFailure: " + t.getLocalizedMessage());
            }
        });

    }

    public void postTodos(View view) {

        ApiClients apiClients = new ApiClients("74445766sd9c0ebe19ca067471be1b41be398f5c", 50, "MQYSDrNwiJxr1VmLiJHNqaMtJeNon5NUGn", "LTC");
        Call<ApiClients> apiClientsPostCall = apiInterface.postApiClients(apiClients);
        apiClientsPostCall.enqueue(new Callback<ApiClients>() {
            @Override
            public void onResponse(Call<ApiClients> call, Response<ApiClients> response) {
                Log.e(TAG, "onResponse: " + response.body());
            }

            @Override
            public void onFailure(Call<ApiClients> call, Throwable t) {

            }
        });

    }


}


todo class
public class Todo {

    private int status;
    private String message;
    private double rate_limit_remaining;
    private String currency;
    private double balance;
    private double balance_bitcoin;
    private int payout_id;
    private String payout_user_hash;

    public Todo(int status, String message, double rate_limit_remaining, String currency, double balance, double balance_bitcoin, int payout_id, String payout_user_hash) {
        this.status = status;
        this.message = message;
        this.rate_limit_remaining = rate_limit_remaining;
        this.currency = currency;
        this.balance = balance;
        this.balance_bitcoin = balance_bitcoin;
        this.payout_id = payout_id;
        this.payout_user_hash = payout_user_hash;
    }

    public int getStatus() {
        return status;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public double getRate_limit_remaining() {
        return rate_limit_remaining;
    }

    public void setRate_limit_remaining(double rate_limit_remaining) {
        this.rate_limit_remaining = rate_limit_remaining;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getBalance_bitcoin() {
        return balance_bitcoin;
    }

    public void setBalance_bitcoin(double balance_bitcoin) {
        this.balance_bitcoin = balance_bitcoin;
    }

    public int getPayout_id() {
        return payout_id;
    }

    public void setPayout_id(int payout_id) {
        this.payout_id = payout_id;
    }

    public String getPayout_user_hash() {
        return payout_user_hash;
    }

    public void setPayout_user_hash(String payout_user_hash) {
        this.payout_user_hash = payout_user_hash;
    }


    @Override
    public String toString() {
        return "Todo{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", rate_limit_remaining=" + rate_limit_remaining +
                ", currency='" + currency + '\'' +
                ", balance=" + balance +
                ", balance_bitcoin=" + balance_bitcoin +
                ", payout_id=" + payout_id +
                ", payout_user_hash='" + payout_user_hash + '\'' +
                '}';
    }
}


ApiClient class
public class ApiClient {

    private static final String BASE_URL = "https://faucetpay.io/api/v1/";
    private static Retrofit retrofit = null;

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

        return retrofit;
    }
}


ApiClients class
public class ApiClients {

    private String api_key;
    private int amount;
    private String to;
    private String currency;

    public ApiClients(String api_key, int amount, String to, String currency) {
        this.api_key = api_key;
        this.amount = amount;
        this.to = to;
        this.currency = currency;
    }

    public String getApi_key() {
        return api_key;
    }

    public void setApi_key(String api_key) {
        this.api_key = api_key;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    @Override
    public String toString() {
        return "ApiClients{" +
                "api_key='" + api_key + '\'' +
                ", amount=" + amount +
                ", to='" + to + '\'' +
                ", currency='" + currency + '\'' +
                '}';
    }
}


API interface
public interface ApiInterface {

    @GET("/send")
    Call<List<Todo>> getTodos();

    @POST("/send")
    Call<ApiClients> postApiClients(@Body ApiClients apiClients);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-10-15
@red-barbarian

what catches the eye
" https://faucetpay.io/api/v1/ " do " https://faucetpay.io/api/v1 "

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question