android - Android 的 Volley 的返回响应
问题描述
我编写了一个函数,它发出一个 HTTP 请求并将响应存储在一个 Bundle 中,以便随后初始化一个活动。
public static void communicate(final Context context, String url, final String typeResponse, final Intent intent) {
RequestQueue queue = Volley.newRequestQueue(context);
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL + url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
switch (typeResponse) {
case "text":
bundle.putString("response", response);
break;
case "json":
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray names = jsonObject.names();
for (int i = 0; i < names.length(); i++) {
//Toast.makeText(context, names.getString(i), Toast.LENGTH_SHORT).show();
bundle.putString(names.getString(i), jsonObject.getString(names.getString(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
intent.putExtras(bundle);
context.startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("test", "hi!!");
return params;
}
};
queue.add(stringRequest);
}
但我想返回 Bundle 对象以使用该函数,如下所示:
Bundle myBundle = communicate('httl://qwe.asd', 'json')
如何修改我的功能?
谢谢。
解决方案
Volley 请求是异步的,因此我建议您将 onResponse 其他函数放入内部来处理您的捆绑包。同样,您可以创建一个界面以在其他地方发送您的响应。像这样的东西
界面
public interface onResponseCallback {
void onResponse(Bundle bundle);
}
活动
public MyActivity extends AppCompatActivity implements onResponseCallback{
public void onCreate(Bundle....){
MyRequest myrequest = new MyRequest(this);
..}
public void onResponse(Bundle bundle){
//bundle argument is your response from request,
// do some with your response
Intent intent = new Intent....
intent.putExtras(bundle);
startActivity(intent);
}
}
请求类
public class MyRequest{
OnResponseCallback onResponseCallback= null;
public MyRequest(onResponseCallback onResponseCallback)
this.onResponseCallback = onResponseCallback;
}
public void communicate(final Context context, String url, final String typeResponse, final Intent intent) {
RequestQueue queue = Volley.newRequestQueue(context);
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL + url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
switch (typeResponse) {
case "text":
bundle.putString("response", response);
break;
case "json":
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray names = jsonObject.names();
for (int i = 0; i < names.length(); i++) {
//Toast.makeText(context, names.getString(i), Toast.LENGTH_SHORT).show();
bundle.putString(names.getString(i), jsonObject.getString(names.getString(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
onResponseCallback.onResponse(bundle);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("test", "hi!!");
return params;
}
};
queue.add(stringRequest);
}
}
如果你不喜欢这个,也许你可以使用常量或放入 sharedpreferences 来保存你的包对象。
我希望这对你有帮助。
推荐阅读
- swiftui - SwiftUI run function when app has been terminated and then opened
- python-3.x - 从 Heroku 仪表板为 Python Telegram 机器人选择“打开应用程序”时出现 HTTP 404 错误
- javascript - 从 POST 操作验证模态后无法正常工作
- swift - SwiftUI - Sheet Dismmis 按钮不起作用
- ruby-on-rails - Rail 6.1 中非默认 Active Storage 服务的优雅回退
- azure - 如何从 Azure Devops Repos 在 Azure App 服务上部署 Gatsby 项目?
- pyspark - 将列加入新的数据框,但仅当存在 customerID 时
- javascript - 在地图函数中使用状态作为 json 键值
- html - 从开发者控制台获取任意 HTML 元素的锚链接
- javascript - 阻止 safari 在 clipboard.write 中修改 HTML