【发布时间】:2021-06-26 15:31:08
【问题描述】:
我在自定义类(非活动类)中获取 API 数据。现在我想将我的 JSON 响应对象解析为 Activity 类,但不能这样做。请教如何将响应对象从非活动类发送到活动类:
注意:我不想使用 Sharedpreference。
我的自定义类:
public class APIHelper {
private static final String baseurl = "myUrl";
private static final String loginAPI = "auth/login";
public void getAPIResponse(Context context, View view, JSONObject parameters) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, baseurl + loginAPI, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//I want to send this response to another activity...
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
JSONObject jsonObject = new JSONObject(res);
Utility.showSnackBar(view, jsonObject.getString("message"), "top", "error");
} catch (UnsupportedEncodingException | JSONException e) {
e.printStackTrace();
}
}
}
});
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(jsonObjectRequest);
}
}
【问题讨论】: