Example usage for com.squareup.okhttp OkHttpClient newCall

List of usage examples for com.squareup.okhttp OkHttpClient newCall

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient newCall.

Prototype

public Call newCall(Request request) 

Source Link

Document

Prepares the request to be executed at some point in the future.

Usage

From source file:com.hippo.nimingban.client.DiscEngine.java

License:Apache License

public static String weiyun(OkHttpClient okHttpClient, String url) throws IOException {
    String body;//from  w  w w.j  a v  a2 s.  c o  m
    Matcher matcher;

    body = okHttpClient.newCall(new Request.Builder().url(url).build()).execute().body().string();
    matcher = PATTERN_WEIYUN_1.matcher(body);
    if (!matcher.find()) {
        throw new IOException("Can't get url");
    }

    body = okHttpClient.newCall(new Request.Builder().url(URL_WEIYUN_OUTLINK)
            .post(new FormEncodingBuilder().add("data", getWeiyunPostJson(matcher.group(1))).build()).build())
            .execute().body().string();
    matcher = PATTERN_WEIYUN_2.matcher(body);
    if (matcher.find()) {
        return matcher.group(1);
    } else {
        throw new IOException("Can't get url");
    }
}

From source file:com.hippo.nimingban.client.DiscEngine.java

License:Apache License

public static String pgyer(OkHttpClient okHttpClient, String url) throws IOException {
    String body;//from w w  w  . java 2  s  . com
    Matcher matcher;

    body = okHttpClient.newCall(new Request.Builder().url(url).build()).execute().body().string();
    matcher = PATTERN_PGYER.matcher(body);
    if (matcher.find()) {
        return "http://www.pgyer.com/app/install/" + matcher.group(1);
    } else {
        throw new IOException("Can't get url");
    }
}

From source file:com.hippo.nimingban.client.UpdateEngine.java

License:Apache License

public static Call prepareUpdate(OkHttpClient okHttpClient) {
    String url = UPDATE_URL;
    Log.d(TAG, url);/*from  www . jav  a  2s.com*/
    Request request = new GoodRequestBuilder(url).build();
    return okHttpClient.newCall(request);
}

From source file:com.hitkoDev.chemApp.rest.LoadDataTask.java

@Override
protected String doInBackground(String... urls) {
    String url = buildURL(urls);/*  ww  w.  j a v a2s. com*/
    file = new File(cache, IOLib.md5(url) + ".json");
    if (checkNetwork()) {
        OkHttpClient client = ChemApp.client;
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    } else if (file.exists()) {
        try (InputStream is = new FileInputStream(file)) {
            return IOLib.readStream(is);
        } catch (Exception ex) {
            return ex.toString();
        }
    } else {
        return "No network or cached files";
    }
}

From source file:com.hitkoDev.chemApp.rest.LoadImageTask.java

@Override
protected Bitmap doInBackground(String... urls) {
    file = new File(cache, IOLib.md5(urls[0]) + ".png");
    if (file.exists()) {
        return BitmapFactory.decodeFile(file.getPath());
    } else if (checkNetwork()) {
        OkHttpClient client = ChemApp.client;
        Request request = new Request.Builder().url(urls[0]).build();
        try {/*www  .j  av a 2 s. c  om*/
            Response response = client.newCall(request).execute();
            Bitmap b = BitmapFactory.decodeStream(response.body().byteStream());
            new StoreFileTask().execute(b);
            return b;
        } catch (Exception ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.hitkoDev.chemApp.rest.SendJSONDataTask.java

@Override
protected String doInBackground(String... urls) {
    if (checkNetwork()) {
        String data = urls[0];/*from   w  w  w  . ja va2s .  c  o  m*/
        String url = buildURL(urls);
        OkHttpClient client = ChemApp.client;
        RequestBody body = RequestBody.create(IOLib.JSON, data);
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    } else {
        return "No network or cached files";
    }
}

From source file:com.hkm.root.Tasks.upload_data.java

License:Open Source License

protected String OCokHttpPostData(final String url, final String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(body).build();
    OkHttpClient use_client = client.clone();
    Response response = use_client.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().string();
    } else/*from  w  w w.j  a va2 s .  c  o  m*/
        throw new IOException("not success on HTTP request.");
}

From source file:com.hkm.root.Tasks.upload_data.java

License:Open Source License

public void execute_upload(Request requestBuild) throws IOException, Exception {
    // executes generic request
    OkHttpClient use_client = client.clone();
    use_client.setWriteTimeout(10, TimeUnit.SECONDS);
    Response response = use_client.newCall(requestBuild).execute();
    if (!response.isSuccessful()) {
        throw new IOException("Unexpected code " + response);
    } else {//  w ww.ja  v  a  2  s.  c om
        if (!response.body().string().isEmpty()) {
            success_events_achieved++;
            // sb.append(response.body().string());
            System.out.println(response.body().string());
            // StandJsonResponse sp = new StandJsonResponse();
            // sp.fromString(response.body().string()).setSingleSuccessAction(success_pass_event).run();
        } else {
            throw new Exception("response body is empty");
        }
    }
}

From source file:com.howareyoudoing.data.net.ApiConnection.java

License:Apache License

private void connectToApi() {
    OkHttpClient okHttpClient = this.createClient();
    final Request request;
    if (body != null) {
        request = new Request.Builder().url(this.url).addHeader(CONTENT_TYPE_LABEL, CONTENT_TYPE_VALUE_JSON)
                .post(body).build();// ww w. j  ava  2 s.  co  m
    } else {
        request = new Request.Builder().url(this.url).addHeader(CONTENT_TYPE_LABEL, CONTENT_TYPE_VALUE_JSON)
                .get().build();
    }
    try {
        this.response = okHttpClient.newCall(request).execute().body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.ibm.mobilefirstplatform.clientsdk.android.core.internal.BaseRequest.java

License:Apache License

protected void sendRequest(final ResponseListener listener, final RequestBody requestBody) {
    if (method == null || !isValidMethod(method)) {
        listener.onFailure(null, new IllegalArgumentException("Method is not valid: " + method), null);
        return;/*w ww .  ja va  2 s.c  o m*/
    }

    Request.Builder requestBuilder = new Request.Builder();

    requestBuilder.headers(headers.build());

    try {
        if (getQueryParamsMap().size() == 0) {
            requestBuilder.url(url);
        } else {
            requestBuilder.url(getURLWithQueryParameters(url, getQueryParamsMap()));

        }
    } catch (MalformedURLException e) {
        listener.onFailure(null, e, null);
        return;
    }

    //A GET request cannot have a body in OKHTTP
    if (!method.equalsIgnoreCase("GET")) {
        requestBuilder.method(method, requestBody);
    } else {
        requestBuilder.get();
    }

    Request request = requestBuilder.build();
    OkHttpClient client = getHttpClient();
    client.newCall(request).enqueue(getCallback(listener));
    client.newCall(request);

}