我正在開發一個android應用程序,它必須對服務器執行多個併發http請求,所以我決定使用okhttp庫來提出請求。同步異步任務
問題是,當我嘗試同時發出某些請求時,我必須等到第一次完成時才執行以下操作。
我創建並運行從我調用以下方法AsyncTasks:
public class ApiRestClient {
public static void get(String url, Map<String, String> params, Api.ApiCallback callback) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).build();
try {
HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();
for (Map.Entry<String, String> e : params.entrySet()) {
builder.addQueryParameter(e.getKey(), e.getValue());
}
Request request = new Request.Builder()
.url(builder.build())
.get()
.build();
Response response = client.newCall(request).execute();
if (response.code() == HttpURLConnection.HTTP_OK) {
JSONObject data = new JSONObject(response.body().string());
if (callback != null) {
try {
try {
callback.onSuccess(data);
} catch (JSONException ex){
throw new ApiException(data, ex);
}
} catch (ApiException ex) {
callback.onFail(ex);
}
}
} else {
if (callback != null) {
callback.onFail(new ApiException(response.code()));
}
}
} catch (JSONException | IOException e) {
Log.e(ApiRestClient.class.getSimpleName(), "ApiRestClient.get", e);
if (callback != null) {
callback.onFail(new ApiException(ApiException.SC_INTERNAL_SERVER_ERROR,e));
}
}
}
public static void post(String url, Map<String, String> params, Api.ApiCallback callback) {
try {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).build();
HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();
FormBody.Builder formBuilder = new FormBody.Builder();
for (Map.Entry<String, String> e : params.entrySet()) {
formBuilder.add(e.getKey(), e.getValue());
}
RequestBody body = formBuilder.build();
Request request = new Request.Builder()
.url(builder.build())
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.code() == HttpURLConnection.HTTP_OK) {
JSONObject data = new JSONObject(response.body().string());
if (callback != null) {
try {
callback.onSuccess(data);
} catch (ApiException ex){
callback.onFail(ex);
}
}
} else {
if (callback != null) {
callback.onFail(new ApiException(response.code()));
}
}
} catch (JSONException | IOException e) {
Log.e(ApiRestClient.class.getSimpleName(), "ApiRestClient.post", e);
if (callback != null) {
callback.onFail(new ApiException(HttpURLConnection.HTTP_INTERNAL_ERROR,e));
}
}
}
}
這是的AsyncTask代碼:
public class ApiMethods extends AsyncTask<String, Integer, Boolean> {
private RequestMethod requestMethod;
private String serverURL;
private Api.ApiCallback callback;
private Map<String, String> requestParams;
public ApiMethods(RequestMethod requestMethod, String serverURL, Map<String, String> requestParams, Api.ApiCallback callback) {
this.requestMethod = requestMethod;
this.serverURL = serverURL;
this.callback = callback;
this.requestParams = requestParams;
}
@Override
protected Boolean doInBackground(String... params) {
switch (this.requestMethod) {
case GET:
ApiRestClient.get(serverURL, requestParams, callback);
break;
case POST:
ApiRestClient.post(serverURL, requestParams, callback);
break;
default:
break;
}
return true;
}
public enum RequestMethod {
GET, POST
}
}