0

I'm using retrofit2 and Rxjava2 to insert/get information from mongodb and nodeJs server, for now, I receive all data as a string but I want to get hole collection Infos from my base so I need to convert string to JSON and get each information.

My code to receive data:

1- Service:

 @POST("collect/get")
    @FormUrlEncoded
    Observable<String> getcollection(@Field("selector") String selector);

2-RetrofitClient:

if(instance == null){
            instance = new Retrofit.Builder()
                    .baseUrl("http://transportor.ddns.net:3000/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create()).build();
        }

3- Recieve function

private void getallcollection(String selector) {

        compositeDisposable.add(myServices.getcollection(selector)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>(){
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d("infos",s);
                    }
                }));
    }

I'm already prepared Collection class:

public class col {
    private String creator;
    private String emailcol;
    private String date_creation_col;
    private String nom_col;
    private String long_col;
    private String lat_col;
    private String tel_fix_col;
    private String tel_mobile_col;
    private String creatorcreator;
    private String heure_matin_col;
    private String heure_apresmatin_col;
    private String type;
    private String imagePath;

    public col(String creator, String emailcol, String date_creation_col, String nom_col, String long_col, String lat_col, String tel_fix_col, String tel_mobile_col, String creatorcreator, String heure_matin_col, String heure_apresmatin_col, String type, String imagePath) {
        this.creator = creator;
        this.emailcol = emailcol;
        this.date_creation_col = date_creation_col;
        this.nom_col = nom_col;
        this.long_col = long_col;
        this.lat_col = lat_col;
        this.tel_fix_col = tel_fix_col;
        this.tel_mobile_col = tel_mobile_col;
        this.creatorcreator = creatorcreator;
        this.heure_matin_col = heure_matin_col;
        this.heure_apresmatin_col = heure_apresmatin_col;
        this.type = type;
        this.imagePath = imagePath;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getEmailcol() {
        return emailcol;
    }

    public void setEmailcol(String emailcol) {
        this.emailcol = emailcol;
    }

    public String getDate_creation_col() {
        return date_creation_col;
    }

    public void setDate_creation_col(String date_creation_col) {
        this.date_creation_col = date_creation_col;
    }

    public String getNom_col() {
        return nom_col;
    }

    public void setNom_col(String nom_col) {
        this.nom_col = nom_col;
    }

    public String getLong_col() {
        return long_col;
    }

    public void setLong_col(String long_col) {
        this.long_col = long_col;
    }

    public String getLat_col() {
        return lat_col;
    }

    public void setLat_col(String lat_col) {
        this.lat_col = lat_col;
    }

    public String getTel_fix_col() {
        return tel_fix_col;
    }

    public void setTel_fix_col(String tel_fix_col) {
        this.tel_fix_col = tel_fix_col;
    }

    public String getTel_mobile_col() {
        return tel_mobile_col;
    }

    public void setTel_mobile_col(String tel_mobile_col) {
        this.tel_mobile_col = tel_mobile_col;
    }

    public String getCreatorcreator() {
        return creatorcreator;
    }

    public void setCreatorcreator(String creatorcreator) {
        this.creatorcreator = creatorcreator;
    }

    public String getHeure_matin_col() {
        return heure_matin_col;
    }

    public void setHeure_matin_col(String heure_matin_col) {
        this.heure_matin_col = heure_matin_col;
    }

    public String getHeure_apresmatin_col() {
        return heure_apresmatin_col;
    }

    public void setHeure_apresmatin_col(String heure_apresmatin_col) {
        this.heure_apresmatin_col = heure_apresmatin_col;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }
}

Actually I received all data and console show me : [{"_id":"5e22074673c926147c3a73f5","date_creation_col":"17-01-2020","creator":"Alaeddine","emailcol":"amir@gmail.com","nom_col":"amir","long_col":"10.179326869547367","lat_col":"36.83353893150942","tel_fix_col":"123","tel_mobile_col":"1234","adress_col":"rue Paris mision 34","heure_matin_col":"7","heure_apresmatin_col":"5","type":"collection","imagePath":"mmmmmmmmmmmm"}]

I want to know how to extract for example creator from this Json.

Andrii Artamonov
  • 622
  • 8
  • 15
bb azeae
  • 87
  • 2
  • 8

1 Answers1

0

You can use a third-party JSON parser, like Google GSON, as you're already developing for Android. Java does not seem to contain a built-in JSON parser.

See this answer.

mkkekkonen
  • 1,704
  • 2
  • 18
  • 35
  • when I use Google GSON as U said like : ` Gson gson = new Gson(); col c = gson.fromJson(s,col.class); Log.d("creator",c.getCreator());` I got Error io.reactivex.exceptions.OnErrorNotImplementedException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ – bb azeae Jan 18 '20 at 22:12
  • It seems that GSON does not accept an array as the outermost JSON element. You could wrap the array into an object like: `String obj = "{\"array\":" + s + "}";` There might be typos but you get the idea... – mkkekkonen Jan 18 '20 at 22:27
  • 1
    I used for solve it converter-gson for retrotit2 like `List heroList = response.body(); List colList = response.body();for ( col c: colList){ Log.d("name : ",c.getNom_col()); }` – bb azeae Jan 18 '20 at 22:58