Android 截击超时错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25994514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Volley Timeout Error
提问by Raju
I am trying to hit a rest service using Volley.
我正在尝试使用 Volley 进行休息服务。
public class AuthFunctions {
private static final String LOGIN_URL = "http://10.0.2.2:8080/stewayservices/user-management/users/10";
boolean result;
public boolean loginUser(String email,String password){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,LOGIN_URL,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JsonObject Response",response.toString());
try {
JSONObject user = response.getJSONObject("user");
String firstName = user.getString("firstName");
if (firstName.equals("Lokesh")){
result = true;
}
else{
result = false;
}
} catch (JSONException e) {
Log.d("Web Service Error",e.getMessage());
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("JsonObject Error Response",volleyError.toString());
}
});
request.setRetryPolicy(new DefaultRetryPolicy(500000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(request);
return result;
}
}
But it is giving me the Volley Timeout error. Below is the Logcat
但它给了我 Volley Timeout 错误。下面是Logcat
D/JsonObject Error Response﹕ com.android.volley.TimeoutError
Please let me know know if I am doing it wrong. This is my first question in stackoverflow regarding Android.
如果我做错了,请告诉我。这是我在 stackoverflow 中关于 Android 的第一个问题。
回答by LeoGalante
This worked for me:
这对我有用:
request.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 50000;
}
@Override
public int getCurrentRetryCount() {
return 50000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
You can change that time.
你可以改变那个时间。
回答by Android Man
com.android.volley.TimeoutError
In 75% cases this error is occured due to connectivity issue '
在 75% 的情况下,此错误是由于连接问题而发生的
if you are testing on localhost or local server
如果您在 localhost 或本地服务器上进行测试
Check firewall is off
检查防火墙是否关闭
回答by Ahamadullah Saikat
String url = "https://api.joind.in/v2.1/events?start=" + start + "&resultsperpage=20&format=json";
Log.i("DREG", "onLoadMoreItems: " + url);
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Add Code Here
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
} else if (error instanceof ServerError) {
} else if (error instanceof AuthFailureError) {
} else if (error instanceof ParseError) {
} else if (error instanceof NoConnectionError) {
} else if (error instanceof TimeoutError) {
Toast.makeText(getContext(),
"Oops. Timeout error!",
Toast.LENGTH_LONG).show();
}
}
}
);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
回答by Vishnu Saini
Add the following after your error listener
在错误侦听器之后添加以下内容
myRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
回答by Avinash
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
} else if (error instanceof ServerError) {
} else if (error instanceof AuthFailureError) {
} else if (error instanceof ParseError) {
} else if (error instanceof NoConnectionError) {
} else if (error instanceof TimeoutError) {
Toast.makeText(getContext(),
"Oops. Timeout error!",
Toast.LENGTH_LONG).show();
}
回答by Meysam Keshvari
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
6000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
);
回答by Kishor Pawar
On Macbook I was running a Django application written in python3.X.
I had to do the following.
在 Macbook 上,我正在运行一个用 python3.X 编写的 Django 应用程序。
我必须执行以下操作。
- Go to system settings
- Go to Security & Privacy
- Select the firewall tab and click on the firewall options
- Allow incoming connection for python 3.7
- 进入系统设置
- 转到安全和隐私
- 选择防火墙选项卡,然后单击防火墙选项
- 允许 python 3.7 的传入连接
回答by WritingForAnroid
Volley throws timeouterror when it couldn't connect to the url provided in request. reasons could be:
当 Volley 无法连接到请求中提供的 url 时,它会抛出 timeouterror。原因可能是:
1)connectivity. 2)Url is not valid.
1)连通性。2) 网址无效。
Try running it on emulator.It should work on emulator as emualator runs on same machine and has same ip as your wamp running on.
尝试在模拟器上运行它。它应该可以在模拟器上运行,因为模拟器运行在同一台机器上,并且与运行的 wamp 具有相同的 ip。
To make it work on real device connect your device to same WLAN as your wampserver is running on. If not connected to same WLAN you have to host your php scripts to web. To do this there are many free web hosting sites like https://www.000webhost.com/are available check them out.
要使其在真实设备上运行,请将您的设备连接到运行 wampserver 的同一 WLAN。如果未连接到同一 WLAN,则必须将 php 脚本托管到网络。为此,有许多免费的网络托管站点,例如https://www.000webhost.com/可以查看它们。
Hope this help!
希望这有帮助!
回答by Red Dot
Check out that your IPv4 Address in the URL is correct and has not been changed.
检查 URL 中的 IPv4 地址是否正确且未更改。
回答by Paulo Lara
The same occurred to me, because I did not started my Xampp. I think may be the same.
我也发生了同样的事情,因为我没有启动我的 Xampp。我想可能是一样的。