Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 send object with POST

i m new about Retrofit Library and i want know what s the better why to send an object with post method of retrofit.

Here is it some code.

My Class:

public class ExampleClass implements Serializable {

    @SerializedName("id")
    int id;


    @SerializedName("name")
    String name;

    public ExampleClass(int id, String name) {
        this.id = id;
        this.name= name;
    }

}

My interface:

public interface ApiInterface {
    @Headers("Content-Type: application/json")
    @POST("getclass/")
    Call<ExampleClass> getExampleClass(@Body ExampleClass exampleClass);
}

My call with retrofit:

......
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<ExampleClass> call = apiService.getExampleClass(exampleClass);
        call.enqueue(new Callback<ExampleClass>() {
            @Override
            public void onResponse(Call<ExampleClass> call, Response<ExampleClass> response) {

                int statusCode = response.code();

                Log.i(TAG, "Status Code: " + statusCode);

            }

            @Override
            public void onFailure(Call<ExampleClass> call, Throwable t) {
                Log.i(TAG, "Error: " + t.toString());
            }

        });

But every time it return me status code 500.

Can someone give me an idea for send an object with retrofit?

like image 454
Marco Ferretti Avatar asked Oct 03 '16 15:10

Marco Ferretti


People also ask

How do you send an object in post request in retrofit?

You can send it with help of @FormUrlEncoded for example : @FormUrlEncoded @Headers("Content-Type: application/json") @POST("getclass/") Call<ExampleClass> getExampleClass(@Field("id") int id, @Field("name") String name); but I think your way is easiest and right one.

How do you pass BODY IN POST request in retrofit Android?

change your call interface @Body parameter to String, don't forget to add @Headers("Content-Type: application/json") : @Headers("Content-Type: application/json") @POST("/api/getUsers") Call<List<Users>> getUsers(@Body String rawJsonString); now you can post raw json.

Does retrofit use reflection?

Retrofit makes use of reflections to get the work done.


2 Answers

You can send it with help of @FormUrlEncoded for example :

@FormUrlEncoded
@Headers("Content-Type: application/json")
@POST("getclass/")
Call<ExampleClass> getExampleClass(@Field("id") int id, @Field("name") String name);

but I think your way is easiest and right one.

If you are not lazy you can find all about retrofit here

like image 133
dara Avatar answered Sep 28 '22 00:09

dara


500 Internal Server Error

The 500 status code, or Internal Server Error, means that server cannot process the request for an unknown reason. Sometimes this code will appear when more specific 5xx errors are more appropriate.

This most common cause for this error is server misconfiguration (e.g. a malformed .htaccess file) or missing packages (e.g. trying to execute a PHP file without PHP installed properly).

You can review it.Itis working fine for me

Api InterFace class

public interface ApiInterface {

    @FormUrlEncoded
        @POST("/billingrest/save")
        Call<LoginError> sendProductList(@Body SaveBill saveBill);

}

SaveBill model

package com.example.dev.billingsoftware.model;

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

import java.util.List;

/**
 * Created by dev on 4/8/17.
 */
public class SaveBill {

    @SerializedName("customer_name")
    @Expose
    private String customerName;
    @SerializedName("customer_id")
    @Expose
    private String customerId;
    @SerializedName("bill_number")
    @Expose
    private String billNumber;
    @SerializedName("mobile_number")
    @Expose
    private String mobileNumber;
    @SerializedName("bill_date")
    @Expose
    private String billDate;
    @SerializedName("address")
    @Expose
    private String address;
    @SerializedName("count")
    @Expose
    private String count;
    @SerializedName("data")
    @Expose
    private List<BillData> data = null;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getBillNumber() {
        return billNumber;
    }

    public void setBillNumber(String billNumber) {
        this.billNumber = billNumber;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public String getBillDate() {
        return billDate;
    }

    public void setBillDate(String billDate) {
        this.billDate = billDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public List<BillData> getData() {
        return data;
    }

    public void setData(List<BillData> data) {
        this.data = data;
    }

}
like image 20
Deven Mer Avatar answered Sep 28 '22 01:09

Deven Mer