如何解决如何在请求中的变量中存储截击响应?
我已经搜索并尝试了许多解决此问题的方法,但是这些方法都对我有用。我想将我用截击请求获取的JSONObject存储在请求中的变量中。我总是打印空对象...我的最后一段代码是:
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public interface RequestListener{
public void onRequest(JSONObject obj);
}
public static JSONObject risultato = new JSONObject();
public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard,container,false);
final RequestQueue requestQueue = Volley.newRequestQueue(root.getContext());
JSONObject data = new JSONObject();
try {
data.put("tipoRichiesta","mie");
data.put("id_utente","1");
} catch (JSONException e) {
e.printStackTrace();
}
final JSONObject[] ris = {new JSONObject()};
RequestListener listener = new RequestListener() {
@Override
public void onRequest(JSONObject obj) {
risultato = obj;
}
};
Log.d("data che invio:",data.toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"http://192.168.1.5/getRichieste.php",data,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// do things on response
listener.onRequest((JSONObject)response);
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Errore",error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
Log.d("result",risultato.toString());
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。