0

I have JSON objects in JSONbin.io in the following structure.

{
  "beneficiaries": [
    {
      "name": "Brian",
      "staff_no": "12",
      "id": "11111111"
    }],
 "users": [
    {
      "national_id": "10102020",
      "sponsor_no": "01",
      "staff_no": "12",
      "name": "Sonya",
      "birth_date": "24-2-1999",
      "kra_pin": "1111111111",
      "employment_date": "10-1-2019",
      "employment_type": "temporary",
      "email": "email@gmail.com",
      "password": "1"
    }]
}

I am attempting, using retrofit, to POST new data entries into users and beneficiaries and so on. However, I cannot seem to figure out the correct endpoint to describe in my jsonBinAPI interface as you can see in the code segment i have posted below. Whenever i run the application, a 404 response code is logged in my logcat and the new record is obviously not POST-ed.

Main Activity

 pojo_user user = new pojo_user(natid,sponsor_no,staff_no,uname,pass);
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("https://api.jsonbin.io/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    jsonBinAPI jsonbinapi = retrofit.create(jsonBinAPI.class);
                    Call<pojo_user> call = jsonbinapi.setUser(user);
                    call.enqueue(new Callback<pojo_user>() {
                        @Override
                        public void onResponse(Call<pojo_user> call, Response<pojo_user> response) {
                            Log.v("register_retrofit",Integer.toString(response.code()));
                        }

                        @Override
                        public void onFailure(Call<pojo_user> call, Throwable t) {
 Log.v("register_retrofit",t.getMessage());
                        }
                    });

pojo_user

public class pojo_user {
    private String national_id;
    private String sponsor_no;
    private String staff_no;
    private String name;
    private String birth_date;
    private String kra_pin;
    private String employment_date;
    private String employment_type;
    private String email;
    private String password;
    private String log_state;

    public pojo_user(String national_id, String sponsor_no, String staff_no, String name, String password) {
        this.national_id = national_id;
        this.sponsor_no = sponsor_no;
        this.staff_no = staff_no;
        this.name = name;
        this.password = password;
    }


}

jsonBinAPI

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;


public interface jsonBinAPI {
    @GET("b/5e536928d3c2f35597f6ca46/5")
    Call<jsonBin_response> getJsonBin_response();
    @Headers({"secret-key: <MY_KEY>","Content-Type: application/json","private: false"})
    @POST("b/5e536928d3c2f35597f6ca46/5/users")
    Call<pojo_user> setUser(@Body pojo_user user);
}

oguz ismail
  • 1
  • 16
  • 47
  • 69

2 Answers2

2

You need to check their documentation first.

I hope you will get clue to solve your question from all the above links.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • I have followed your links and have edited my code accordingly. However, i am getting a 404 error. Can you have a look at my edited question and see if you can find what I am missing? –  Mar 02 '20 at 08:15
1

Looking at the docs, it doesn't seem possible to add a new object into an existing bin directly. You may achieve this, though, by creating two collections here - one for users and another for beneficiaries. Then add a user bin to the users collection using

   @Headers({
       "Content-Type: application/json",
       "secret-key: <Your secret key>",
       "collection-id: <Collection-ID for users>"
   })
   @POST('b')
   Call<pojo_user> setUser(@Body pojo_user user);

And something similar for beneficiaries.

The route provided in the docs for creating a bin is just /b. You provided b/5e536928d3c2f35597f6ca46/5/users. This may be why you're getting a 404.