com.squareup.okhttp.Callback Java Examples

The following examples show how to use com.squareup.okhttp.Callback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OkHttpClientAsyncInstrumentationTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Override
protected void performGet(String path) throws Exception {
    Request request = new Request.Builder()
        .url(path)
        .build();

    final CompletableFuture<Void> future = new CompletableFuture<>();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request req, IOException e) {
            future.completeExceptionally(e);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            try {
                response.body().close();
            } finally {
                future.complete(null);
            }
        }
    });
    future.get();

}
 
Example #2
Source File: OkHttpStack.java    From AndNet with Apache License 2.0 6 votes vote down vote up
@Override
public void post(final String url, final RequestParams params,
                 final Net.Parser<T> parser,
                 final Net.Callback<T> callback,
                 final Object tag) {
    MultipartBuilder builder = createRequestBody(params);
    Request request = new Request.Builder()
            .url(url).post(builder.build()).build();
    call(request, parser, callback, tag);
}
 
Example #3
Source File: LoginPresenter.java    From Social with Apache License 2.0 6 votes vote down vote up
public void loginWithQQopenid(final Activity activity){
    OkhttpUtil.getIns().loginWithQQopenid(mOpenId, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d(TAG, "onFailure: ");
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {
            //NOT UI Thread
            String result = response.body().string();
            handleLogin(activity, result, Constants.HX_DEFAULT_PASSWORD);
            mViewRef.get().loginSuccess();
        }
    });
}
 
Example #4
Source File: AggregatorService.java    From spring-async with MIT License 6 votes vote down vote up
public void execute(final Task task) {
    log.info("Started task with {} urls", task.getUrls().size());
    task.start();
    for(int i = 0; i < task.getUrls().size(); i++) {
        final int index = i;
        final long time = System.currentTimeMillis();
        String url = task.getUrls().get(i);
        Request req = new Request.Builder().get().url(url).build();

        client.newCall(req).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                task.fail(index, time, request, e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                task.success(index, time, response);
            }
        });
    }
}
 
Example #5
Source File: AptoideUtils.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
public static void startParse(final String storeName, final Context context, final SpiceManager spiceManager) {
    Toast.makeText(context, AptoideUtils.StringUtils.getFormattedString(context, R.string.subscribing, storeName), Toast.LENGTH_SHORT).show();

    GetSimpleStoreRequest request = new GetSimpleStoreRequest();
    request.store_name = storeName;

    CheckSimpleStoreListener checkStoreListener = new CheckSimpleStoreListener();
    checkStoreListener.callback = new CheckSimpleStoreListener.Callback() {
        @Override
        public void onSuccess() {
            addStoreOnCloud(storeName, context, spiceManager);
        }
    };

    spiceManager.execute(request, checkStoreListener);
}
 
Example #6
Source File: OkHttpClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeEnqueue(@Advice.Origin Class<? extends Call> clazz,
                                    @Advice.FieldValue(value = "originalRequest", typing = Assigner.Typing.DYNAMIC, readOnly = false) @Nullable Request originalRequest,
                                    @Advice.Argument(value = 0, readOnly = false) @Nullable Callback callback,
                                    @Advice.Local("span") Span span) {
    if (tracer == null || tracer.getActive() == null || callbackWrapperCreator == null) {
        return;
    }

    final WrapperCreator<Callback> wrapperCreator = callbackWrapperCreator.getForClassLoaderOfClass(clazz);
    if (originalRequest == null || callback == null || wrapperCreator == null) {
        return;
    }

    final AbstractSpan<?> parent = tracer.getActive();

    Request request = originalRequest;
    URL url = request.url();
    span = HttpClientHelper.startHttpClientSpan(parent, request.method(), url.toString(), url.getProtocol(),
        OkHttpClientHelper.computeHostName(url.getHost()), url.getPort());
    if (span != null) {
        span.activate();
        if (headerSetterHelperManager != null) {
            TextHeaderSetter<Request.Builder> headerSetter = headerSetterHelperManager.getForClassLoaderOfClass(Request.class);
            if (headerSetter != null) {
                Request.Builder builder = originalRequest.newBuilder();
                span.propagateTraceContext(builder, headerSetter);
                originalRequest = builder.build();
            }
        }
        callback = wrapperCreator.wrap(callback, span);
    }
}
 
Example #7
Source File: OkHttpStack.java    From AndNet with Apache License 2.0 5 votes vote down vote up
@Override
public void delete(final String url, final Net.Parser<T> parser,
                   final Net.Callback<T> callback,
                   final Object tag) {
    final Request request = new Request.Builder()
            .url(url).delete().build();
    call(request, parser, callback, tag);
}
 
Example #8
Source File: MainActivity.java    From TutosAndroidFrance with MIT License 5 votes vote down vote up
public void post(){

        MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
        String myJson = "{}";

        //post Request
        Request myGetRequest = new Request.Builder()
                .url("https://api.github.com/users/florent37")
                .post(RequestBody.create(JSON_TYPE, myJson))
                .build();

        okHttpClient.newCall(myGetRequest).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                //le retour est effectué dans un thread différent
                final String text = response.body().string();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(text);
                    }
                });
            }
        });
    }
 
Example #9
Source File: RefreshAccessTokenTask.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RefreshAccessTokenTask(final Callback mCallBack, final String mRefreshToken, String clientId, String code){
    this.mOkHttpClient = new OkHttpClient();
    this.mCallBack = mCallBack;
    this.mRefreshToken = mRefreshToken;
    this.mClientId = clientId;
    this.mCode = code;
}
 
Example #10
Source File: FindWiFiImpl.java    From find-client-android with MIT License 5 votes vote down vote up
AuthTask(String urlPart, String serverAddr, int method, String json, Callback callback) {
    this.urlPart = urlPart;
    this.serverAddr = serverAddr;
    this.method = method;
    this.json = json;
    this.callback = callback;
}
 
Example #11
Source File: ListProjectsTask.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ListProjectsTask(Context context, String url, Callback callback, String token) {
    this.mContext = context;
    this.url = url;
    this.callback = callback;
    httpClient = new OkHttpClient();
    this.mToken = token;
}
 
Example #12
Source File: MainActivity.java    From TutosAndroidFrance with MIT License 5 votes vote down vote up
public void get(){
    //get Request
    Request myGetRequest = new Request.Builder()
            .url("https://api.github.com/users/florent37")
            .build();

    okHttpClient.newCall(myGetRequest).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {
            //le retour est effectué dans un thread différent
            final String text = response.body().string();
            final int statusCode = response.code();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(text);
                }
            });
        }
    });
}
 
Example #13
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void getNewsList(String requestPage, final NetCallback netCallback) {
    OkhttpUtil.getIns().getNewsList(requestPage, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(), netCallback);
        }
    });
}
 
Example #14
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void getUserList(String requestPage, final NetCallback netCallback) {
    OkhttpUtil.getIns().getUserList(requestPage, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            netCallback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(), netCallback);
        }
    });
}
 
Example #15
Source File: OkHttpAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public OkHttpListenableFuture(Call call) {
	this.call = call;
	this.call.enqueue(new Callback() {
		@Override
		public void onResponse(Response response) {
			set(new OkHttpClientHttpResponse(response));
		}
		@Override
		public void onFailure(Request request, IOException ex) {
			setException(ex);
		}
	});
}
 
Example #16
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void autoLogin(final NetCallback netCallback) {
    Log.d(TAG, "autoLogin: ");
    OkhttpUtil.getIns().autoLogin(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            netCallback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(), netCallback);
        }
    });
}
 
Example #17
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void login(String username, String password, final NetCallback netCallback) {
    OkhttpUtil.getIns().login(username, password, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            netCallback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(),netCallback);
        }
    });
}
 
Example #18
Source File: RequestAccessTokenTask.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RequestAccessTokenTask(final Activity activity, final Callback mCallBack, final String url, String clientId, String code){
    this.mActivity = activity;
    this.mOkHttpClient = new OkHttpClient();
    this.mCallBack = mCallBack;
    this.url = url;
    this.mClientId = clientId;
    this.mCode = code;
}
 
Example #19
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void pullRefreshUser(final NetCallback netCallback) {
    OkhttpUtil.getIns().pullRefreshUser(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            netCallback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(), netCallback);
        }
    });
}
 
Example #20
Source File: OkHttpService.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void likeNews(String newsId, final NetCallback netCallback) {
    OkhttpUtil.getIns().likeNews(newsId, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            netCallback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleStringResponse(response.body().string(), netCallback);
        }
    });
}
 
Example #21
Source File: AuthTaskUrlShortener.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public AuthTaskUrlShortener(final Callback mCallBack, final String longUrl, Activity context, Account account, JSONObject jsonObject, String requestType){
    this.mOkHttpClient = new OkHttpClient();
    this.mCallBack = mCallBack;
    this.longUrl = longUrl;
    this.mActivity = context;
    this.mAccount = account;
    this.mJsonObject = jsonObject;
    this.mRequestType = requestType;
}
 
Example #22
Source File: FindWiFiImpl.java    From find-client-android with MIT License 5 votes vote down vote up
AuthTask(String urlPart, String serverAddr, int method, String json, Callback callback) {
    this.urlPart = urlPart;
    this.serverAddr = serverAddr;
    this.method = method;
    this.json = json;
    this.callback = callback;
}
 
Example #23
Source File: OkHttpClientHttpRequest.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public OkHttpListenableFuture(Call call) {
     this.call = call;
     this.call.enqueue(new Callback() {
@Override
      public void onResponse(Response response) {
       set(new OkHttpClientHttpResponse(response));
      }
      @Override
      public void onFailure(Request request, IOException ex) {
       setException(ex);
      }
     });
 }
 
Example #24
Source File: ScienceDetailsActivity.java    From Leisure with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void onDataRefresh() {

    Utils.getRawHtmlFromUrl(articleBean.getUrl(), new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d(TAG, "onFailure: " + e.getMessage());
        }

        @Override
        public void onResponse(final Response response) throws IOException {
            final String rawData = response.body().string();
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    ScienceContentParser myParse = new ScienceContentParser(rawData);
                    String data = myParse.getEndStr();
                    scrollView.setVisibility(View.VISIBLE);
                    scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                        @Override
                        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                            topImage.setTranslationY(Math.max(-scrollY / 2, -DisplayUtil.dip2px(getBaseContext(), 170)));
                        }
                    });
                    contentView.loadDataWithBaseURL("file:///android_asset/", "<link rel=\"stylesheet\" type=\"text/css\" href=\"guokr.css\" />" + data, "text/html", "utf-8", null);
                }
            });
        }
    });
    if(HttpUtil.isWIFI == true || Settings.getInstance().getBoolean(Settings.NO_PIC_MODE, false) == false) {
        setMainContentBg(articleBean.getImage_info().getUrl());
    }

    hideLoading();
}
 
Example #25
Source File: UploadFileRequest.java    From lunzi with Apache License 2.0 5 votes vote down vote up
private void newRequestCall(Callback callback, String url, RequestBody requestBody) {
    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(callback);
}
 
Example #26
Source File: OkHttpStack.java    From AndNet with Apache License 2.0 5 votes vote down vote up
@Override
public void get(final String url, final Net.Parser<T> parser,
                final Net.Callback<T> callback,
                final Object tag) {
    final Request request = new Request.Builder()
            .url(url).build();
    call(request, parser, callback, tag);
}
 
Example #27
Source File: OkHttpStack.java    From AndNet with Apache License 2.0 5 votes vote down vote up
@Override
public void post(final String url, final String json,
                 final Net.Parser<T> parser,
                 final Net.Callback<T> callback, final Object tag) {
    Request request = new Request.Builder()
            .url(url).post(createJsonBody(json)).build();
    call(request, parser, callback, tag);
}
 
Example #28
Source File: OkHttpStack.java    From AndNet with Apache License 2.0 5 votes vote down vote up
@Override
public void put(final String url, final String json,
                final Net.Parser<T> parser,
                final Net.Callback<T> callback, final Object tag) {
    Request request = new Request.Builder()
            .url(url).put(createJsonBody(json)).build();
    call(request, parser, callback, tag);
}
 
Example #29
Source File: Utils.java    From Leisure with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void getRawHtmlFromUrl(String url, Callback callback) {
    if (callback == null || TextUtils.isEmpty(url)) {
        return ;
    }
    Request.Builder builder = new Request.Builder();
    builder.url(url);
    Request request = builder.build();
    HttpUtil.enqueue(request, callback);
}
 
Example #30
Source File: ProximityBeaconImpl.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
AuthTask(String urlPart, int method, String json, Callback callback) {
  this.urlPart = urlPart;
  this.method = method;
  this.json = json;
  this.callback = callback;
}