一般而言,我真的是html requset和Web服务方面的noobie,所以我需要一些有关正在开发的Android应用程序的帮助。因此,情况是这样的,我想进行POST调用(改造2)并将JSON响应的特定值保存在某些变量中。问题是我想同时保存JSON对象中的值和JSON对象中包含的JSON数组中的值。以下是POST请求:
{
"service": "login",
"username": "MyUser",
"password": "123456",
"appId": "3001"
}
以下是POST请求的响应。我需要保存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"
}
下面您将在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) {
}
});
}
}
界面
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;
}
}
所以这是我从这里找到的方法:https://www.freshbytelabs.com/2018/12/parse-json-array-inside-json-object-in.html
但是我不能在main活动中调用该方法...位于Obj类中的粗体部分。-> loginCompany = response.body()。getObjs。getCOMPANY();我只能获得clientID
如果有人可以帮助我,我将非常感谢
您的问题的解决方法实际上很简单:getObjs()
方法返回List<Obj>
,因此您不能直接访问Obj
的属性。
例如,如果您只对您可以写的第一个条目感兴趣:
response.body().getObjs().get(0).getCOMPANY();
当然,您应该事先检查列表中是否确实包含元素。要正确处理此问题,您可以编写:
List<Obj> objs = response.body().getObjs();
if (objs.size() > 0) {
String company = objs.get(0).getCOMPANY();
}