本文整理汇总了Java中com.squareup.okhttp.Response.isSuccessful方法的典型用法代码示例。如果您正苦于以下问题:Java Response.isSuccessful方法的具体用法?Java Response.isSuccessful怎么用?Java Response.isSuccessful使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.Response
的用法示例。
在下文中一共展示了Response.isSuccessful方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleResponse
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
/**
* Handle the given response, return the deserialized object when the response is successful.
*
* @param <T> Type
* @param response Response
* @param returnType Return type
* @throws ApiException If the response has a unsuccessful status code or
* fail to deserialize the response body
* @return Type
*/
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
if (response.isSuccessful()) {
if (returnType == null || response.code() == 204) {
// returning null if the returnType is not defined,
// or the status code is 204 (No Content)
return null;
} else {
return deserialize(response, returnType);
}
} else {
String respBody = null;
if (response.body() != null) {
try {
respBody = response.body().string();
} catch (IOException e) {
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
}
}
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
}
}
示例2: getResponseJson
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
/**
* @param client
* @param request
* @return
* @throws Exception
*/
private String getResponseJson(OkHttpClient client, Request request) throws Exception {
if (client == null || request == null) {
Log.e(TAG, "getResponseJson client == null || request == null >> return null;");
return null;
}
Response response = client.newCall(request).execute();
return response.isSuccessful() ? response.body().string() : null;
}
示例3: execute2String
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
public String execute2String(Request request) {
String result = null;
try {
Response response = okHttpClient.newCall(request).execute();
if (response != null && response.isSuccessful()) {
result = response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
示例4: getStringFromServer
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
public static String getStringFromServer(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
Response response = execute(request);
if (response.isSuccessful()) {
String responseUrl = response.body().string();
return responseUrl;
} else {
throw new IOException("Unexpected code " + response);
}
}
示例5: onSuccess
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
@Override
public void onSuccess(Response response) {
if(response.isSuccessful()){
WXSDKManager.getInstance().postOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(WXEnvironment.sApplication,"Has switched to DEBUG mode, you can see the DEBUG information on the browser!",Toast.LENGTH_SHORT).show();
}
},0);
}
}
示例6: save
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
public static long save(String collection, long lastRead, String user, OAuthAuthorization oauth) {
try {
collection = "lists." + Long.parseLong(collection);
} catch (NumberFormatException ignored) {
}
try {
final String auth = generateVerifyCredentialsAuthorizationHeader(TWITTER_VERIFY_CREDENTIALS_JSON, oauth);
JSONObject body = new JSONObject();
JSONObject collectionJson = new JSONObject();
collectionJson.put("id", lastRead);
body.put(collection, collectionJson);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
.url(TWEETMARKER_API_URL + "?api_key=" + API_KEY + "&username=" + user)
.addHeader("X-Auth-Service-Provider", TWITTER_VERIFY_CREDENTIALS_JSON)
.addHeader("X-Verify-Credentials-Authorization", auth)
.post(RequestBody.create(JSON, body.toString()))
.build();
final Response response = createHttpClientWithoutSSL().newCall(request).execute();
if (response.isSuccessful()) return lastRead;
} catch (JSONException | IOException | KeyManagementException | NoSuchAlgorithmException e) {
Timber.i(e, "");
}
return -1;
}
示例7: toWebResource
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
protected WebResourceResponse toWebResource(Response response) throws IOException {
if (response == null || !response.isSuccessful()) {
return null;
}
String mimeType = response.header("Content-Type");
String charset = response.header("charset");
return new WebResourceResponse(mimeType, charset, response.body().byteStream());
}
示例8: onResponse
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
public void onResponse(Response response) {
if (response.isSuccessful()) {
BufferedSink buffer = Okio.buffer(Okio.sink(this.b));
buffer.writeAll(response.body().source());
buffer.close();
this.a.onSuccess();
return;
}
this.a.onFailure(0, "download failed");
}
示例9: getStringFromServer
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
/**
* 发起请求
* @param url
* @return
* @throws IOException
*/
public static String getStringFromServer(String url) throws IOException{
Request request = new Request.Builder().url(url).build();
Response response = execute(request);
if (response.isSuccessful()) {
String responseUrl = response.body().string();
return responseUrl;
}
else {
throw new IOException("Unexpected code " + response);
}
}
示例10: requestNewToken
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
private void requestNewToken(String grantType) throws IOException {
Map<String, String> localVarHeaderParams = new HashMap<>();
localVarHeaderParams.put("Accept", "application/json");
String method = "GET";
String[] localVarAuthNames = new String[] {};
List<Pair> localVarQueryParams = new ArrayList<>();
Map<String, Object> formParams = new HashMap<>();
localVarQueryParams.addAll(apiClient.parameterToPairs("", "grant_type", grantType));
if ("refresh_token".equals(grantType) || "password".equals(grantType)) {
String client = authenticationProperties.get(KEY_CLIENT);
String secret = authenticationProperties.get(KEY_SECRET);
byte[] creds = (client + ":" + secret).getBytes();
localVarHeaderParams.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(creds));
}
if ("refresh_token".equals(grantType)) {
localVarQueryParams.addAll(apiClient.parameterToPairs("", "refresh_token", currentToken.getRefreshToken()));
}
if ("password".equals(grantType)) {
localVarHeaderParams.put("Content-Type", "multipart/form-data");
method = "POST";
formParams.put("username", authenticationProperties.get(KEY_USERNAME));
formParams.put("password", authenticationProperties.get(KEY_PASSWORD));
}
Response response = null;
try {
Request r = apiClient.buildRequest("/oauth/token", method, localVarQueryParams, null, localVarHeaderParams, formParams, localVarAuthNames, null);
logger.debug(r.toString());
Call call = apiClient.buildCall(r);
response = call.execute();
if (response.isSuccessful()) {
MyOAuthToken newToken = apiClient.deserialize(response, myOAuthTokenType);
newToken.computeExpirationDate();
currentToken = newToken;
} else {
throw new IOException("Unable to get an authentication token (" + response.code() + ") : Msg(" + response.message() + ") Body("
+ (response.body() != null ? response.body().string() : "") + ")");
}
} catch (ApiException e) {
throw new IOException(e);
} finally {
if (response != null) {
response.body().close();
}
}
}