Android 使用期货的截击请求超时
我想使用Volley库创建对REST服务的同步请求。 我尝试使用Future对象和15秒超时设置“Helper”来管理请求,这是我的代码:Android 使用期货的截击请求超时,android,rest,android-volley,Android,Rest,Android Volley,我想使用Volley库创建对REST服务的同步请求。 我尝试使用Future对象和15秒超时设置“Helper”来管理请求,这是我的代码: import android.app.Application; import android.content.Context; import android.content.OperationApplicationException; import android.util.Log; import com.android.volley.NetworkRes
import android.app.Application;
import android.content.Context;
import android.content.OperationApplicationException;
import android.util.Log;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.json.JSONObject;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class RestHelper {
private static RestHelper instance = null;
private static String L="LOG";
public static synchronized RestHelper getInstance(Context context)
{
if (null == instance)
instance = new RestHelper(context);
return instance;
}
private RequestQueue requestQueue;
private RestHelper(Context context)
{
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
private JsonElement get(String url) throws InterruptedException, ExecutionException, TimeoutException {
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(url, null, future, future);
requestQueue.add(request);
JSONObject obj;
try{
obj=future.get(15, TimeUnit.SECONDS);
}catch (InterruptedException | ExecutionException e){
if(e.getCause() instanceof VolleyError) {
NetworkResponse networkResponse = ((VolleyError) e.getCause()).networkResponse;
Log.e(L,"Network error, HTTP status: "+networkResponse.statusCode);
Log.e(L,"Network error, HTTP content: "+new String(networkResponse.data));
}else
Log.e(L,"Network error: "+e.getMessage());
throw e;
}catch (TimeoutException e){
Log.e(L,"Network timeout");
throw e;
}
return parseResponse(obj);
}
private JsonElement parseResponse(JSONObject obj){
JsonParser parser = new JsonParser();
JsonElement mJson = parser.parse(obj.toString());
Gson gson = new Gson();
Return ret = gson.fromJson(mJson, Return.class);
if(!ret.isSuccess())
throw new RuntimeException(ret.getError());
return mJson;
}
}
导入android.app.Application;
导入android.content.Context;
导入android.content.OperationApplicationException;
导入android.util.Log;
导入com.android.volley.NetworkResponse;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.RequestFuture;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入com.google.gson.gson;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonParser;
导入com.google.gson.reflect.TypeToken;
导入org.json.JSONObject;
导入java.util.List;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.TimeUnit;
导入java.util.concurrent.TimeoutException;
公共类RestHelper{
私有静态RestHelper实例=null;
私有静态字符串L=“LOG”;
公共静态同步RestHelper getInstance(上下文)
{
if(null==实例)
实例=新的RestHelper(上下文);
返回实例;
}
私有请求队列请求队列;
私有RestHelper(上下文)
{
requestQueue=Volley.newRequestQueue(context.getApplicationContext());
}
私有JsonElement get(字符串url)抛出InterruptedException、ExecutionException、TimeoutException{
RequestFuture=RequestFuture.newFuture();
JsonObjectRequest=新的JsonObjectRequest(url,null,future,future);
添加(请求);
JSONObject对象;
试一试{
obj=未来.get(15,时间单位.秒);
}捕获(中断异常|执行异常e){
if(例如getCause()截击错误实例){
NetworkResponse NetworkResponse=((截击错误)e.getCause()).NetworkResponse;
Log.e(L,“网络错误,HTTP状态:“+networkResponse.statusCode”);
Log.e(L,“网络错误,HTTP内容:”+新字符串(networkResponse.data));
}否则
Log.e(L,“网络错误:+e.getMessage());
投掷e;
}捕获(超时异常e){
Log.e(L,“网络超时”);
投掷e;
}
返回应答(obj);
}
私有JsonElement parseResponse(JSONObject对象){
JsonParser=新的JsonParser();
JsonElement mJson=parser.parse(obj.toString());
Gson Gson=新的Gson();
Return ret=gson.fromJson(mJson,Return.class);
如果(!ret.issucess())
抛出新的RuntimeException(ret.getError());
返回mJson;
}
}
远程端点被调用(我已经在远程服务器上找到了连接日志),但是未来的对象总是被标记为超时
我读过一些关于线程问题的文章,但据我所知,
RequestQueue
是使用主应用程序上下文创建的。够了吗?我还缺少什么?你打电话给startQueue了吗?从你的代码你没有它
基于
Volley.newRequestQueue(context.getApplicationContext())
没有启动队列?@Tobia它是。刚刚连接到不同的创建实例RequestQueue
有不同的文档。我尝试了,但没有改变。未来仍处于超时状态……:-(
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);
// Start the queue
requestQueue.start();