List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
From source file:com.xjeffrose.xio2.http.server.ServerTest.java
License:Apache License
@Test(expected = SSLException.class) public void testSslFail() throws Exception { OkHttpClient unsafeClient = getUnsafeOkHttpClient(); s.serve(9007, testHandler);//w w w . j a va 2s . c o m Request request = new Request.Builder().url("https://localhost:9007/test").build(); Response response = unsafeClient.newCall(request).execute(); assertTrue(!response.isSuccessful()); }
From source file:com.xjeffrose.xio2.http.server.ServiceTest.java
License:Apache License
@Test public void testHandleGet() throws Exception { HttpHandler testHandler = new HttpHandler(); testHandler.addRoute("/service_test", new TestHttpService()); s.serve(9011, testHandler);/* w ww .ja v a 2 s. c o m*/ Request request = new Request.Builder().url("http://localhost:9011/service_test").build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); assertTrue(response.isSuccessful()); assertEquals(response.code(), 200); }
From source file:com.yandex.disk.rest.RestClientIO.java
License:Apache License
Operation getOperation(String url) throws IOException, HttpCodeException { Response response = call(METHOD_GET, url); int code = response.code(); if (!response.isSuccessful()) { throw new HttpCodeException(code); }/* ww w.j ava2 s .c o m*/ return parseJson(response, Operation.class); }
From source file:com.yingke.shengtai.adapter.ChatAllHistoryAdapter.java
License:Open Source License
public void postRequest(final TextView text, final String userName, final Map<String, String> map) { FormEncodingBuilder builder = new FormEncodingBuilder(); for (Map.Entry<String, String> entry : map.entrySet()) { builder.add(entry.getKey(), entry.getValue()); }/*from w ww. j a va 2 s .c o m*/ RequestBody formBody = builder.build(); Request request = new Request.Builder().url(IApi.URL_IMID_NAME).header("User-Agent", "OkHttp Headers.java") .addHeader("Accept", "application/json").addHeader("Content-Type", "application/json;charset=utf-8") .addHeader("Content-Length", "length").post(formBody).build(); RequestManager.getOkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String json = response.body().string(); ImidToNickNameData data = JosnUtil.gson.fromJson(json, new TypeToken<ImidToNickNameData>() { }.getType()); if (data != null && TextUtils.equals("1", "1")) { try { User user = new User(); user.setNick(data.getDetaillist().get(0).getDisplayname()); user.setUsername(data.getDetaillist().get(0).getImid()); user.setSex(data.getDetaillist().get(0).getSex()); userDao.saveContactsss(user); UIHandler.sendEmptyMessage(0); } catch (Exception e) { } } } } }); }
From source file:com.yingke.shengtai.adapter.ChatSaleAdapter.java
License:Open Source License
public void postRequest(final TextView text, final String userName, final Map<String, String> map) { FormEncodingBuilder builder = new FormEncodingBuilder(); for (Map.Entry<String, String> entry : map.entrySet()) { builder.add(entry.getKey(), entry.getValue()); }// w w w . j a v a 2 s.com RequestBody formBody = builder.build(); Request request = new Request.Builder().url(IApi.URL_IMID_NAME).header("User-Agent", "OkHttp Headers.java") .addHeader("Accept", "application/json").addHeader("Content-Type", "application/json;charset=utf-8") .addHeader("Content-Length", "length").post(formBody).build(); RequestManager.getOkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String json = response.body().string(); ImidToNickNameData data = JosnUtil.gson.fromJson(json, new TypeToken<ImidToNickNameData>() { }.getType()); if (data != null && TextUtils.equals("1", data.getResult() + "")) { try { User user = new User(); user.setNick(data.getDetaillist().get(0).getDisplayname()); user.setUsername(data.getDetaillist().get(0).getImid()); user.setSex(data.getDetaillist().get(0).getSex()); userDao.saveContactsss(user); UIHandler.sendEmptyMessage(0); } catch (Exception e) { } } } } }); }
From source file:com.zachklipp.jfavicon.OkHttpHttpClient.java
License:Apache License
@Override public String tryLoadBody(URL url) { String body = null;//from w w w . j a va2s . c om try { final Request request = new Request.Builder().url(url).get().build(); final Response response = client.newCall(request).execute(); if (response.isSuccessful()) { body = response.body().string(); } } catch (IOException e) { } return body; }
From source file:de.dev.eth0.rssreader.core.http.callback.AbstractFeedLoaderCallback.java
License:Apache License
@Override public void onResponse(Response response) throws IOException { Timber.d("onSuccess %d (cache: %s network: %s)", response.code(), response.cacheResponse(), response.networkResponse()); if (response.isSuccessful() && response.networkResponse() != null && response.networkResponse().code() != HttpURLConnection.HTTP_NOT_MODIFIED) { List<FeedEntry> summaries = RssFeedParser.parseFeed(response.body().byteStream()); DataProviderHelper helper = getDataProviderHelper(); List<FeedEntry> added = helper.insertFeedEntry(summaries); getNotificationManger().sendFeedUpdatedNotification(added); getPrecacheHelper().precache(summaries); }/*from w ww. j a va2 s . c o m*/ afterCallback(); }
From source file:de.schildbach.wallet.data.ExchangeRatesProvider.java
License:Open Source License
private Map<String, ExchangeRate> requestExchangeRates() { final Stopwatch watch = Stopwatch.createStarted(); final Request.Builder request = new Request.Builder(); request.url(BITCOINAVERAGE_URL); request.header("User-Agent", userAgent); final Call call = Constants.HTTP_CLIENT.newCall(request.build()); try {//from w w w . j a v a 2s.c om final Response response = call.execute(); if (response.isSuccessful()) { final String content = response.body().string(); final JSONObject head = new JSONObject(content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); if (currencyCode.startsWith("BTC")) { final String fiatCurrencyCode = currencyCode.substring(3); if (!fiatCurrencyCode.equals(MonetaryFormat.CODE_BTC) && !fiatCurrencyCode.equals(MonetaryFormat.CODE_MBTC) && !fiatCurrencyCode.equals(MonetaryFormat.CODE_UBTC)) { final JSONObject exchangeRate = head.getJSONObject(currencyCode); final JSONObject averages = exchangeRate.getJSONObject("averages"); try { final Fiat rate = parseFiatInexact(fiatCurrencyCode, averages.getString("day")); if (rate.signum() > 0) rates.put(fiatCurrencyCode, new ExchangeRate( new org.bitcoinj.utils.ExchangeRate(rate), BITCOINAVERAGE_SOURCE)); } catch (final IllegalArgumentException x) { log.warn("problem fetching {} exchange rate from {}: {}", currencyCode, BITCOINAVERAGE_URL, x.getMessage()); } } } } watch.stop(); log.info("fetched exchange rates from {}, {} chars, took {}", BITCOINAVERAGE_URL, content.length(), watch); return rates; } else { log.warn("http status {} when fetching exchange rates from {}", response.code(), BITCOINAVERAGE_URL); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + BITCOINAVERAGE_URL, x); } return null; }
From source file:de.schildbach.wallet.ExchangeRatesProvider.java
License:Open Source License
private static Map<String, ExchangeRate> requestExchangeRates(final URL url, final String userAgent, final String source, final String... fields) { final Stopwatch watch = Stopwatch.createStarted(); final Request.Builder request = new Request.Builder(); request.url(url);/*from w ww.j av a 2 s.co m*/ request.header("User-Agent", userAgent); final Call call = Constants.HTTP_CLIENT.newCall(request.build()); try { final Response response = call.execute(); if (response.isSuccessful()) { final String content = response.body().string(); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); final JSONObject head = new JSONObject(content); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = Strings.emptyToNull(i.next()); if (currencyCode != null && !"timestamp".equals(currencyCode) && !MonetaryFormat.CODE_BTC.equals(currencyCode) && !MonetaryFormat.CODE_MBTC.equals(currencyCode) && !MonetaryFormat.CODE_UBTC.equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); for (final String field : fields) { final String rateStr = o.optString(field, null); if (rateStr != null) { try { final Fiat rate = Fiat.parseFiat(currencyCode, rateStr); if (rate.signum() > 0) { rates.put(currencyCode, new ExchangeRate( new org.bitcoinj.utils.ExchangeRate(rate), source)); break; } } catch (final NumberFormatException x) { log.warn("problem fetching {} exchange rate from {}: {}", currencyCode, url, x.getMessage()); } } } } } watch.stop(); log.info("fetched exchange rates from {}, {} chars, took {}", url, content.length(), watch); return rates; } else { log.warn("http status {} when fetching exchange rates from {}", response.code(), url); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + url, x); } return null; }
From source file:de.schildbach.wallet.ui.AlertDialogsFragment.java
License:Open Source License
@Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); log.debug("querying \"{}\"...", versionUrl); final Request.Builder request = new Request.Builder(); request.url(versionUrl);/*from w w w. j a v a 2s . c o m*/ request.header("Accept-Charset", "utf-8"); final String userAgent = application.httpUserAgent(); if (userAgent != null) request.header("User-Agent", userAgent); final Call call = Constants.HTTP_CLIENT.newCall(request.build()); backgroundHandler.post(new Runnable() { @Override public void run() { boolean abort = false; try { final Response response = call.execute(); if (response.isSuccessful()) { final long serverTime = response.headers().getDate("Date").getTime(); try (final BufferedReader reader = new BufferedReader(response.body().charStream())) { abort = handleServerTime(serverTime); while (true) { final String line = reader.readLine(); if (line == null) break; if (line.charAt(0) == '#') continue; final Splitter splitter = Splitter.on('=').trimResults(); final Iterator<String> split = splitter.split(line).iterator(); if (!split.hasNext()) continue; final String key = split.next(); if (!split.hasNext()) { abort = handleLine(key); if (abort) break; continue; } final String value = split.next(); if (!split.hasNext()) { abort = handleProperty(key, value); if (abort) break; continue; } log.info("Ignoring line: {}", line); } } } } catch (final Exception x) { handleException(x); } if (!abort) handleCatchAll(); } }); }