POST 请求后保存 JSON 响应的特定值
Posted
技术标签:
【中文标题】POST 请求后保存 JSON 响应的特定值【英文标题】:Save specific values of JSON response after POST request 【发布时间】:2020-08-07 09:41:15 【问题描述】:总的来说,我在 html 请求集和 Web 服务方面真的很菜鸟,所以我需要一些关于我正在开发的 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
但我不能在主要活动中调用该方法...位于 Obj 类中的粗体部分。 --> loginCompany = response.body().getObjs.getCOMPANY(); 我只能获取clientID
如果有人可以帮助我,我将非常感激
【问题讨论】:
【参考方案1】:您的问题的解决方案其实很简单:
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();
【讨论】:
谢谢。这就是问题所在。所以让我问你一些事情...对于每个不同的发布/获取请求,我是否必须为每个响应(与上述情况不同的格式)设置类...? 如果您的请求返回不同的对象,那么可以。否则你可以使用现有的类。以上是关于POST 请求后保存 JSON 响应的特定值的主要内容,如果未能解决你的问题,请参考以下文章
Ameritrade API POST 请求 JSON 响应