简体   繁体   中英

Save specific values of JSON response after POST request

I am really noobie in html requsets and web services in general, so i need a little help with the Android app which i am developing. So the case is that, i want to make a POST call(with retrofit 2) and save in some variables specific values of the JSON response. The problem is that i want to save both values from a JSON object and values from JSON array that is included in the JSON object. Below is the POST request:

{
"service": "login",
"username": "MyUser",
"password": "123456",
"appId": "3001"
}

Below is the response of the POST request. I need to save clientID, but also values(COMPANY and REFID) from array objs

 {
"success": true,
"clientID": "9J8pJ6La…Ga5mHG",
"objs": [ {
"COMPANY": "1000",
"COMPANYNAME": "Standard Company",
"BRANCH": "1000",
"BRANCHNAME": "Head Offices",
"MODULE": "0",
"MODULENAME": "<Blank>",
"REFID": "500",
"REFIDNAME": "demo",
"USERID": "500",
"FINALDATE": "1899-12-30 00:00:00",
"ROLES": ""
}],
"ver": "4.00.514.10630",
"sn": "01234567890123"
}

Below you will find the Java code in Android Studio Let's say that this My Main activity

package com.example.semswms;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginAuthentication extends AppCompatActivity {
    private TextView textv3, textv4;

    public static String loginService="login";
    public static String loginUsername="sems";
    public static String loginPassword="sems17674";
    public static String appID="9999";
    public static String loginSuccess="";
    public static String loginClientID="";
    public static String loginCompany="";
    public static String loginBranch="";
    public static String loginModule="";
    public static String loginRefID="";
    public static String loginSN="";

    public static String authService="authenticate";
    public static String authSuccess="";
    public static String authClientID="";

    String url= "http://xxx.xxxxx.com/";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.itedoc);
        super.onCreate(savedInstanceState);
        textv3 = findViewById(R.id.textView3);
        textv4 = findViewById(R.id.textView4);
        login();
    }

    private void login(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        SoftoneAPI softoneAPI = retrofit.create(SoftoneAPI.class);
        S1Connection s1Connection = new S1Connection(loginService,loginUsername,loginPassword,appID);
        Call<S1Connection> call = softoneAPI.postlogin(s1Connection);
        call.enqueue(new Callback<S1Connection>() {
            @Override
            public void onResponse(Call<S1Connection> call, Response<S1Connection> response) {
                if (! response.isSuccessful()){
                    Toast.makeText(getApplicationContext(),"Request Failed",Toast.LENGTH_LONG).show();

                }
                    loginClientID = response.body().getClientID();
                    loginCompany = response.body().getObjs().toString();
                    textv3.setText(loginCompany);
                    textv4.setText(loginClientID);
            }

            @Override
            public void onFailure(Call<S1Connection> call, Throwable t) {

            }
        });
    }
}

the interface

package com.example.semswms;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface SoftoneAPI {
    @POST("s1services")
    Call<S1Connection> postlogin(@Body S1Connection s1Connection);

}

package com.example.semswms;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class S1Connection {

    @SerializedName("success")
    @Expose
    private Boolean success;
    @SerializedName("clientID")
    @Expose
    private String clientID;
    @SerializedName("objs")
    @Expose
    private List<Obj> objs = null;
    @SerializedName("ver")
    @Expose
    private String ver;
    @SerializedName("sn")
    @Expose
    private String sn;

    @SerializedName("service")
    @Expose
    public String service;
    @SerializedName("username")
    @Expose
    public String username;
    @SerializedName("password")
    @Expose
    public String password;
    @SerializedName("appId")
    @Expose
    public String appId;

    public S1Connection(String service, String username, String password, String appId) {
        this.service = service;
        this.username = username;
        this.password = password;
        this.appId = appId;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getClientID() {
        return clientID;
    }

    public void setClientID(String clientID) {
        this.clientID = clientID;
    }

    public List<Obj> getObjs() {
        return objs;
    }

    public void setObjs(List<Obj> objs) {
        this.objs = objs;
    }

    public String getVer() {
        return ver;
    }

    public void setVer(String ver) {
        this.ver = ver;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

}

package com.example.semswms;

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

public class Obj {

    @SerializedName("COMPANY")
    @Expose
    private String cOMPANY;
    @SerializedName("COMPANYNAME")
    @Expose
    private String cOMPANYNAME;
    @SerializedName("BRANCH")
    @Expose
    private String bRANCH;
    @SerializedName("BRANCHNAME")
    @Expose
    private String bRANCHNAME;
    @SerializedName("MODULE")
    @Expose
    private String mODULE;
    @SerializedName("MODULENAME")
    @Expose
    private String mODULENAME;
    @SerializedName("REFID")
    @Expose
    private String rEFID;
    @SerializedName("REFIDNAME")
    @Expose
    private String rEFIDNAME;
    @SerializedName("USERID")
    @Expose
    private String uSERID;
    @SerializedName("FINALDATE")
    @Expose
    private String fINALDATE;
    @SerializedName("ROLES")
    @Expose
    private String rOLES;

    public String getCOMPANY() {
        return cOMPANY;
    }

    public void setCOMPANY(String cOMPANY) {
        this.cOMPANY = cOMPANY;
    }

    public String getCOMPANYNAME() {
        return cOMPANYNAME;
    }

    public void setCOMPANYNAME(String cOMPANYNAME) {
        this.cOMPANYNAME = cOMPANYNAME;
    }

    public String getBRANCH() {
        return bRANCH;
    }

    public void setBRANCH(String bRANCH) {
        this.bRANCH = bRANCH;
    }

    public String getBRANCHNAME() {
        return bRANCHNAME;
    }

    public void setBRANCHNAME(String bRANCHNAME) {
        this.bRANCHNAME = bRANCHNAME;
    }

    public String getMODULE() {
        return mODULE;
    }

    public void setMODULE(String mODULE) {
        this.mODULE = mODULE;
    }

    public String getMODULENAME() {
        return mODULENAME;
    }

    public void setMODULENAME(String mODULENAME) {
        this.mODULENAME = mODULENAME;
    }

    public String getREFID() {
        return rEFID;
    }

    public void setREFID(String rEFID) {
        this.rEFID = rEFID;
    }

    public String getREFIDNAME() {
        return rEFIDNAME;
    }

    public void setREFIDNAME(String rEFIDNAME) {
        this.rEFIDNAME = rEFIDNAME;
    }

    public String getUSERID() {
        return uSERID;
    }

    public void setUSERID(String uSERID) {
        this.uSERID = uSERID;
    }

    public String getFINALDATE() {
        return fINALDATE;
    }

    public void setFINALDATE(String fINALDATE) {
        this.fINALDATE = fINALDATE;
    }

    public String getROLES() {
        return rOLES;
    }

    public void setROLES(String rOLES) {
        this.rOLES = rOLES;
    }

}

So this is the approach i found from here: https://www.freshbytelabs.com/2018/12/parse-json-array-inside-json-object-in.html

but i cant call the method in the main activity...the bold part which is located in Obj class. --> loginCompany = response.body().getObjs. getCOMPANY() ; I can only get clientID

I would be very grateful if someone could help me

The solution to your problem is actually simple: The getObjs() method returns a List<Obj> therefore you cannot directly access a property of Obj .

If, for example, you are only interested in the first entry you could write:

response.body().getObjs().get(0).getCOMPANY();

Of course you should check beforehand if the list does in fact have an element. To properly handle this you could write:

List<Obj> objs = response.body().getObjs();

if (objs.size() > 0) {
   String company = objs.get(0).getCOMPANY();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM