0

I want post json request like that

{"jsonrpc": "2.0", "method": "testApi", "params": { "message": "abc" } , "id": 1}

I read posts:

How to POST raw whole JSON in the body of a Retrofit request?

but i can'not found class TypedInput, TypedByteArray, TypedString in my retrofit2 package. Where is?

Community
  • 1
  • 1
Vladislav Aniskin
  • 319
  • 1
  • 4
  • 10

2 Answers2

3

POST data to the server require backhend program which is post your data to the database in the server.....

Retrofit Post require RESTAPI and POJO Class ....

API Interface

public interface Api {
    @POST("/upload/{new}.json")
    Call<User> setData(@Path("new") String s1, @Body User user);
}

Retrofit Object

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("here-your-url")
                .addConverterFactory(GsonConverterFactory.create()) 
                .build(); 

APi object

Api api = retrofit.create(Api.class);

Retrofit Call

Call<User> call = api.setData("mahesh", new User("mahesh", "delhi"));
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            t1.setText("Success");
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.d("sam", "fail");
            t1.setText("fail");
        }
    });

POJO class //this class you create just put your json data into this POJOConvertion

public class User {

    String name;
    String address;

    public User(String name, String address) {
        this.address = address;
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

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

enjoy coding.

If you want any practice for the Retrofit than please use this Retrofit + Firebase

sushildlh
  • 8,986
  • 4
  • 33
  • 77
2

To POST a body in Retrofit, you create an object that represents this body, a class that includes String jsonrpc, String method, etc. Then, pass this object to the method that you define in your service interface and has a param with @Body annotation.

Here is an example for POST body object:

public class PostBody{

   String jsonprc;
   String method;
   Param param;

   public PostBody(...){
        //IMPLEMENT THIS
   }

   ...

   class Param{
       //IMPLEMENT THIS
   }

}
Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73