我想传递来自 POST 在API上完成的请求的JSON数据响应。我创建了实现Parcelable类的Sharing类。我想利用parcelable类在主要活动中保留几个对象(客户信息) JSON 响应,并将它们发送到第二个活动中以仅显示客户信息。这是我各自的代码,
这是api响应。.{
"success": "true",
"message": "Logged in successfuly",
"user": {
"id": 13,
"userNo": "",
"name": "Adam",
"username": "Adam@gmail.com",
"actualPassword": "12345",
"email": "apollo@client.com",
"secondaryEmail": null,
"primaryPhone": 9876544345,
"secondaryPhone": null,
"clientId": {
"clientId": 1,
"name": "Charlie",
"address": "India",
"createdBy": null,
"createdAt": "2018-10-25T11:25:19.000Z",
"updatedAt": "2019-01-21T10:10:39.000Z",
"is_active": 1,
"clientCode": "APL",
"startTime": "08:00:00.000000",
"endTime": "07:59:59.000000",
},
"gender": null,
"dob": null,
"emergencyMobile": null,
"officeNo": null,
"loggedInStatus": 0,
}
}
这是我的主要活动。
public class MainActivity extends AppCompatActivity {
public static String myUrl="IP Address URL Link";
TextView tvIsConnected;
EditText etEmail;
EditText etPassword;
TextView tvResult;
Button btn_Send;
String name="";
TextView client_ID;
TextView client_Name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
tvResult = (TextView) findViewById(R.id.tvResult);
btn_Send =(Button)findViewById(R.id.btnSend);
final HTTPAsyncTask process = new HTTPAsyncTask();
btn_Send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
process.execute();
}
});
}
@SuppressLint("ParcelCreator")
public class HTTPAsyncTask extends AsyncTask<String, Void, String> implements Parcelable {
JSONObject jsonObject = new JSONObject();
Intent i = new Intent(MainActivity.this, DashboardActivity.class);
@Override
protected String doInBackground(String... urls) {
StringBuffer output = new StringBuffer();
try {
try {
URL url = new URL(myUrl);
jsonObject.put("userName", etEmail.getText().toString());
jsonObject.put("password", etPassword.getText().toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStream os = conn.getOutputStream();
os.write(jsonObject.toString().getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String temp;
while ((temp = br.readLine()) != null) {
output.append(temp);
}
conn.disconnect();
} catch (JSONException e) {
e.printStackTrace();
return "Error";
}
}
catch (IOException e) {
return "Error while retrieving screen";
}
catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
@Override
public void onPostExecute(String result ) {
i.putExtra("key",result);
startActivity(i);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
}
以下是我的Sharing类,它实现了Parcelable类。
public class SharingClass implements Parcelable {
private int ClientId ;
private String ClientName;
private String ClientAddress;
public SharingClass(){
super();
}
public SharingClass(Parcel parcel){
this.ClientId = parcel.readInt();
this.ClientName = parcel.readString();
this.ClientAddress = parcel.readString();
}
public SharingClass(Parcelable sharedObject) {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(ClientId);
dest.writeString(ClientName);
dest.writeString(ClientAddress);
}
public static final Creator<SharingClass> CREATOR = new Creator<SharingClass>() {
@Override
public SharingClass createFromParcel(Parcel parcel) {
return new SharingClass(parcel);
}
@Override
public SharingClass[] newArray(int i) {
return new SharingClass[i];
}
};
public void setClientId(int ClientId) {
this.ClientId = ClientId;
}
public void setClientName(String ClientName) {
this.ClientName = ClientName;
}
public void setClientAddress(String ClientAddress) {
this.ClientAddress = ClientAddress;
}
public int getClientId() {
return ClientId;
}
public String getClientName() {
return ClientName;
}
public String getClientAddress() {
return ClientAddress;
}
}
请给我一个解决方案,以访问JSON数据以在第二个活动中显示客户端信息,并在我刚接触Android时显示解析情况
答案 0 :(得分:0)
由于您已将对象实现了可打包的,因此只需使用putExtra()将它们放入您的Intent中即可。
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
然后您可以使用getParcelableExtra():
将其拉出Intent i = getIntent();
SharingClass sharingClass = (SharingClass) i.getParcelableExtra("name_of_extra");
,如果您必须访问POJO内部的数据,则可以使用getter方法获取数据,例如您可以使用getter方法。如果必须访问ClientId,则可以通过以下方式完成。
int clientId = sharingClass.getClientId();
希望这会有所帮助。