![](/img/trans.png)
[英]How to fix "com.android.Volley.TimeoutError" in android while trying to perform a registration activity
[英]Why i'm getting error?com.android.volley.timeouterror
大家好,有人可以幫我上這門課嗎? 我收到一個錯誤,我只是 android studio 和移動開發的新手。 我正在學習和獲取知識的過程中。 請幫我。
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Register.this, response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(NAME, pangaran);
params.put(SURNAME, apelyido);
params.put(ADDRESS, lugar);
params.put(CONTACTNO, kontak);
params.put(USERNAME, username1);
params.put(PASSWORD, password1);
return params;
}
};
requestQueue.add(stringRequest);
也許您可以增加超時以檢查一切正常工作。 由於您之前說過您的代碼對您有用,可能的原因可能是您的服務器響應您的請求所花費的時間較少,由於流量的原因,這些請求可能始終不相同。
嘗試將以下幾行添加到您現有的代碼中。
stringRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 30000;
}
@Override
public int getCurrentRetryCount() {
return 30000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
看到這個鏈接,希望它會幫助你設置截擊的時間。
如果它沒有幫助,請告訴我。
最后我插入了代碼來更改由 volley 設置的默認時間,您可以自己設置時間,它應該以毫秒為單位。
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Register.this, response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(NAME, pangaran);
params.put(SURNAME, apelyido);
params.put(ADDRESS, lugar);
params.put(CONTACTNO, kontak);
params.put(USERNAME, username1);
params.put(PASSWORD, password1);
return params;
}
};
//10000 is the time in milliseconds adn is equal to 10 sec
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
對我來說:那只是為了你的“VPN”,關掉你的“VPN”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.