Search code examples
androidrx-javaretrofit2rx-android

Convert json response to string android


In my application, I get some response from the server in json, and then I have to get some certain string data from that response. As I got to know it will be better for me to use rxjava for my purposes. Firstly, I inserted some dependencies into my gradle, like this:

implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

But then as I understood I have to change my interface request from call to observable, however after it my interface doesn't work. So this question is still opened)) Anyway I have already read a lot of tutorials and all of them use observable, but I hope that maybe I will manage to solve my difficulties without it. And then the main question is - how I have to initialize my converter in my mainactivity class. I saw that sometimes we can get some fields which were in our answer class:

public class Post {
    @SerializedName("username")
    private String username;
    @SerializedName("password")
    private String password;

    public Post(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

But I don't need to get my password or username from the following class, I have to get my access token from my response and then insert it into my header programmatically. My initialization of Retrofit:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://server/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

Anyway thank you for your useful comments and answers, I hope that you will help me.


Solution

  • You can get data from String like this :

                       public void onResponse(String response) {
                            System.out.println("---------------- Responce : " + response);
                        try{
                            JSONArray obj=new JSONArray(response);
    
                            for(int i=0;i<obj.length();i++) {
                                JSONObject jobj = obj.getJSONObject(i);
                                jobj.get("created_at").toString());
                                jobj.get("is_read").toString().equals("1"))
                                jobj.get("first_name").toString(),
                                jobj.get("last_name").toString(),
                                jobj.get("lastmsg").toString(),
                                jobj.get("profile_img").toString(),
                                jobj.get("online").toString(),
                                jobj.get("unread").toString()));
                            }catch (JSONException e){}
    

    }