Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.os.AsyncTask;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Random;
- import java.util.Scanner;
- import javax.net.ssl.HttpsURLConnection;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public abstract class RequestSender extends AsyncTask<Void, Void, Object> {
- public static final String URL = "https://wingstaxi.ru/api/";
- public static final String ENCODING = "UTF-8";
- private JSONObject request;
- protected URL getURL() throws MalformedURLException {
- return new URL(URL);
- }
- protected int getReadTimeout() { return READ_TIMEOUT; }
- /**
- * Формирует запрос с подписью, который будет отправлен на сервер
- */
- protected JSONObject onFormRequest() throws Throwable {
- return null;
- }
- /**
- * Вызывается когда ответ успешно получен с сервера
- */
- protected abstract void onResponseReceived(JSONObject request, JSONObject response) throws Throwable;
- /**
- * Вызывается при ошибке в формировании/отправке/обработке запроса
- */
- protected void onRequestFailed(JSONObject request, Throwable cause) {
- cause.printStackTrace();
- }
- @Override
- protected final Object doInBackground(Void[] args) {
- try {
- request = onFormRequest();
- return send(request);
- } catch(Throwable e) {
- try { Thread.sleep(1000); }
- catch (InterruptedException ex) { ex.printStackTrace(); }
- return e;
- }
- }
- @Override
- protected final void onPostExecute(Object result) {
- if(result instanceof JSONObject) {
- try {
- onResponseReceived(request, (JSONObject)result);
- } catch(Throwable e) {
- onRequestFailed(request, e);
- }
- } else {
- onRequestFailed(request, (Throwable)result);
- }
- }
- protected JSONObject send(JSONObject request) throws Throwable {
- HttpsURLConnection conn = null;
- try {
- conn = (HttpsURLConnection) getURL().openConnection();
- conn.setReadTimeout(getReadTimeout());
- conn.setConnectTimeout(CONNECT_TIMEOUT);
- conn.setRequestMethod(request == null ? "GET" : "POST");
- conn.setDoInput(true);
- conn.setDoOutput(request != null);
- conn.connect();
- if(Thread.interrupted()) throw new InterruptedException();
- if(request != null) {
- conn.getOutputStream().write(request.toString().getBytes(ENCODING));
- conn.getOutputStream().close();
- }
- int responseCode = conn.getResponseCode();
- if(responseCode == HttpsURLConnection.HTTP_OK) {
- StringBuilder sb = new StringBuilder();
- Scanner sc = new Scanner(conn.getInputStream(), ENCODING);
- while (sc.hasNextLine()) {
- sb.append(sc.nextLine());
- sb.append('\n');
- }
- if(Thread.interrupted()) throw new InterruptedException();
- conn.disconnect();
- return new JSONObject(sb.toString());
- } else {
- throw new IOException("Bad response: " + responseCode);
- }
- } catch(Throwable e) {
- if(conn != null) conn.disconnect();
- throw e;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement