gpt4 book ai didi

android - 第一次没有得到服务器 android volley 的响应

转载 作者:行者123 更新时间:2023-11-29 00:55:36 25 4
gpt4 key购买 nike

java类中的代码:

public String POST(String url, final Map<String, String> params, final VolleyCallBack callBack) {

//RequestFuture<String> requestFuture = new RequestFuture.newFuture();
RequestFuture<String> future = RequestFuture.newFuture();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading....");
progressDialog.show();
StringRequest stringRequest = new StringRequest(
Request.Method.POST,
url,

new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
progressDialog.dismiss();

val = jsonObject.getString("message");
callBack.onSuccess();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
ToastMsg(error.getMessage() + " " + error.getStackTrace()[0]);
}
}
) {
@Override
protected Map<String, String> getParams() {
return params;
}
};

RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(mRetryPolicy);

RequestQueue requestQueue = Volley.newRequestQueue(context);
stringRequest.setShouldCache(false);
requestQueue.add(stringRequest);


return val;
}

Activity 类中的代码:

val = db.POST(Constants.URL_FacultyLogin, map);

当我第一次点击按钮时,我没有收到服务器的响应,但第二次我收到了响应,谁能帮我解决为什么不是第一次。

最佳答案

解决方案是使用asynctask Refer this

 @Override
protected String doInBackground(String... params) {
//Call your volley function here
}



@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//The result variable holds your val returned from volley
}

关于android - 第一次没有得到服务器 android volley 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55036651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com