[英]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.一般来说,我在 html 请求和 web 服务方面真的很菜鸟,所以我需要一些关于我正在开发的 Android 应用程序的帮助。 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.
所以情况是,我想进行 POST 调用(使用 retrofit 2)并将 JSON 响应的特定值保存在一些变量中。 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.
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:
以下是 POST 请求:
{
"service": "login",
"username": "MyUser",
"password": "123456",
"appId": "3001"
}
Below is the response of the POST request.下面是 POST 请求的响应。 I need to save clientID, but also values(COMPANY and REFID) from array objs
我需要保存 clientID,还需要保存数组 objs 中的值(COMPANY 和 REFID)
{
"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下面您将在 Android Studio 中找到 Java 代码假设这是我的主要活动
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所以这是我从这里找到的方法: 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.但我不能在主要活动中调用该方法......位于 Obj class 中的粗体部分。 --> loginCompany = response.body().getObjs.
--> loginCompany = response.body().getObjs。 getCOMPANY() ;
获取公司() ; I can only get clientID
我只能得到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
.您的问题的解决方案实际上很简单:
getObjs()
方法返回一个List<Obj>
因此您不能直接访问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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.