本文整理汇总了Java中com.squareup.okhttp.Request类的典型用法代码示例。如果您正苦于以下问题:Java Request类的具体用法?Java Request怎么用?Java Request使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Request类属于com.squareup.okhttp包,在下文中一共展示了Request类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createOpenCellIdApiCall
import com.squareup.okhttp.Request; //导入依赖的package包/类
private Request createOpenCellIdApiCall() {
StringBuilder sb = new StringBuilder();
sb.append("http://www.opencellid.org/cell/get?key=").append(CellTracker.OCID_API_KEY);
if (mAimsicdService.getCell().getMCC() != Integer.MAX_VALUE) {
sb.append("&mcc=").append(mAimsicdService.getCell().getMCC());
}
if (mAimsicdService.getCell().getMNC() != Integer.MAX_VALUE) {
sb.append("&mnc=").append(mAimsicdService.getCell().getMNC());
}
if (mAimsicdService.getCell().getLAC() != Integer.MAX_VALUE) {
sb.append("&lac=").append(mAimsicdService.getCell().getLAC());
}
if (mAimsicdService.getCell().getCID() != Integer.MAX_VALUE) {
sb.append("&cellid=").append(mAimsicdService.getCell().getCID());
}
sb.append("&format=json");
return new Request.Builder()
.url(sb.toString())
.get()
.build();
}
示例2: fetch
import com.squareup.okhttp.Request; //导入依赖的package包/类
@Override
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) {
fetchState.submitTime = SystemClock.uptimeMillis();
final Uri uri = fetchState.getUri();
try {
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noStore().build())
.url(uri.toString())
.get()
.build();
fetchWithRequest(fetchState, callback, request);
} catch (Exception e) {
// handle error while creating the request
callback.onFailure(e);
}
}
示例3: shouldSendGetRequestAndReceiveException
import com.squareup.okhttp.Request; //导入依赖的package包/类
@Test
public void shouldSendGetRequestAndReceiveException() {
final Request request = new com.squareup.okhttp.Request.Builder()
.url(URI)
.method("GET", null)
.build();
when(okHttpClient.newCall(argThat(new RequestMatcher(request)))).thenReturn(call);
doAnswer(invocation -> {
final Callback callback = invocation.getArgument(0);
callback.onFailure(request, new IOException());
return Void.TYPE;
}).when(call).enqueue(isA(Callback.class));
final CompletionStage<com.spotify.apollo.Response<ByteString>> response =
client.send(com.spotify.apollo.Request.forUri(URI, "GET").withTtl(Duration.ofMillis(100)));
verify(okHttpClient).setReadTimeout(100, TimeUnit.MILLISECONDS);
assertTrue(response.toCompletableFuture().isCompletedExceptionally());
}
示例4: doInBackground
import com.squareup.okhttp.Request; //导入依赖的package包/类
@Override
protected Void doInBackground(Void... params) {
try {
Request.Builder requestBuilder = new Request.Builder()
.url(serverAddr + urlPart);
switch (method) {
case PUT:
requestBuilder.put(RequestBody.create(MEDIA_TYPE_JSON, json));
break;
case POST:
requestBuilder.post(RequestBody.create(MEDIA_TYPE_JSON, json));
break;
case DELETE:
requestBuilder.delete(RequestBody.create(MEDIA_TYPE_JSON, json));
break;
default: break;
}
Request request = requestBuilder.build();
httpClient.newCall(request).enqueue(new HttpCallback(callback));
} catch (Exception e) {
Log.e(TAG, "IOException", e);
}
return null;
}
示例5: testWildfly
import com.squareup.okhttp.Request; //导入依赖的package包/类
/**
* Run some HTTP queries against a Docker container from image promagent/wildfly-kitchensink-promagent.
* <p/>
* The Docker container is started by the maven-docker-plugin when running <tt>mvn verify -Pwildfly</tt>.
*/
@Test
public void testWildfly() throws Exception {
OkHttpClient client = new OkHttpClient();
Request restRequest = new Request.Builder().url(System.getProperty("deployment.url") + "/rest/members").build();
// Execute REST call
Response restResponse = client.newCall(restRequest).execute();
Assertions.assertEquals(restResponse.code(), 200);
Assertions.assertTrue(restResponse.body().string().contains("John Smith"));
Thread.sleep(100); // metric is incremented after servlet has written the response, wait a little to get the updated metric
assertMetrics(client, "1.0");
// Execute REST call again
restResponse = client.newCall(restRequest).execute();
Assertions.assertEquals(restResponse.code(), 200);
Assertions.assertTrue(restResponse.body().string().contains("John Smith"));
Thread.sleep(100); // metric is incremented after servlet has written the response, wait a little to get the updated metric
assertMetrics(client, "2.0");
}
示例6: doInBackground
import com.squareup.okhttp.Request; //导入依赖的package包/类
/**
*
* @param params
* @return
*/
@Override
protected String doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient();
HttpUrl httpUrl = HttpUrl.parse(COIN_DESK_API_URL);
//System.out.println("Requesting : " + httpUrl.toString());
FormEncodingBuilder formBody = new FormEncodingBuilder();
formBody.add("lastHours", "24");
formBody.add("maxRespArrSize", "24");
Request request = new Request.Builder()
.url(httpUrl)
.post(formBody.build())
.build();
String content = null;
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
if (isZipped(response)) {
content = unzip(body);
} else {
content = body.string();
}
body.close();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
示例7: connect
import com.squareup.okhttp.Request; //导入依赖的package包/类
public void connect(String url) {
try {
mHttpClient= (OkHttpClient) Class.forName("com.squareup.okhttp.OkHttpClient").newInstance();
} catch (Exception e) {
isSupportWebSocket =false;
return;
}
mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
mHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
// Disable timeouts for read
mHttpClient.setReadTimeout(0, TimeUnit.MINUTES);
Request request = new Request.Builder().url(url).build();
WebSocketCall call = WebSocketCall.create(mHttpClient, request);
call.enqueue(this);
}
示例8: onOpen
import com.squareup.okhttp.Request; //导入依赖的package包/类
@Override
public void onOpen(WebSocket webSocket, Request arg1, Response arg2)
throws IOException {
mWebSocket = webSocket;
setEnvironment(WXEnvironment.getConfig());
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);
for (JSDebuggerCallback callback : mCallbacks.values()) {
callback.onSuccess(arg2);
}
WXLogUtils.e("into--[onOpen]");
}
示例9: onOkHttpNew
import com.squareup.okhttp.Request; //导入依赖的package包/类
@Around("call(* com.squareup.okhttp.OkHttpClient+.newCall(..))")
public Object onOkHttpNew(ProceedingJoinPoint joinPoint) throws Throwable {
if (!Configuration.httpMonitorEnable || joinPoint.getArgs().length != 1) {
return joinPoint.proceed();
}
Object[] args = joinPoint.getArgs();
Request request = (Request) args[0];
//url
URL url = request.url();
if (GlobalConfig.isExcludeHost(url.getHost())) {
return joinPoint.proceed();
}
RespBean bean = new RespBean();
bean.setUrl(url.toString());
bean.setStartTimestamp(System.currentTimeMillis());
startTimeStamp.add(bean);
return joinPoint.proceed();
}
示例10: get
import com.squareup.okhttp.Request; //导入依赖的package包/类
public static long get(String collection, String user) {
try {
collection = "lists." + Long.parseLong(collection);
} catch (NumberFormatException ignored) {
}
Request request = new Request.Builder()
.url(TWEETMARKER_API_URL + "?api_key=" + API_KEY + "&username=" + user + "&collection" + collection)
.get()
.build();
try {
final Response response = createHttpClientWithoutSSL().newCall(request).execute();
JSONObject json = new JSONObject(response.body().string());
return json.getJSONObject(collection).getLong("id");
} catch (IOException | JSONException | NoSuchAlgorithmException | KeyManagementException e) {
Timber.i(e, "");
}
return -1;
}
示例11: writeRequestHeaders
import com.squareup.okhttp.Request; //导入依赖的package包/类
public void writeRequestHeaders(Request request) throws IOException {
if (this.stream == null) {
List<Header> requestHeaders;
this.httpEngine.writingRequestHeaders();
boolean permitsRequestBody = this.httpEngine.permitsRequestBody(request);
if (this.framedConnection.getProtocol() == Protocol.HTTP_2) {
requestHeaders = http2HeadersList(request);
} else {
requestHeaders = spdy3HeadersList(request);
}
this.stream = this.framedConnection.newStream(requestHeaders, permitsRequestBody, true);
this.stream.readTimeout().timeout((long) this.httpEngine.client.getReadTimeout(),
TimeUnit.MILLISECONDS);
this.stream.writeTimeout().timeout((long) this.httpEngine.client.getWriteTimeout(),
TimeUnit.MILLISECONDS);
}
}
示例12: post
import com.squareup.okhttp.Request; //导入依赖的package包/类
Call post(Callback callback) throws IOException {
OkHttpClient client = getUnsafeOkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
RequestBody requestBody = new FormEncodingBuilder()
.add("user_id", NetId)
.add("user_password", password)
.build();
Request request = new Request.Builder()
.url("https://studentmaintenance.webapps.snu.edu.in/students/public/studentslist/studentslist/loginauth")
.post(requestBody)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
示例13: networkRequest
import com.squareup.okhttp.Request; //导入依赖的package包/类
private Request networkRequest(Request request) throws IOException {
Request.Builder result = request.newBuilder();
if (request.header("Host") == null) {
result.header("Host", Util.hostHeader(request.httpUrl()));
}
if (request.header("Connection") == null) {
result.header("Connection", "Keep-Alive");
}
if (request.header(AsyncHttpClient.HEADER_ACCEPT_ENCODING) == null) {
this.transparentGzip = true;
result.header(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient.ENCODING_GZIP);
}
CookieHandler cookieHandler = this.client.getCookieHandler();
if (cookieHandler != null) {
OkHeaders.addCookies(result, cookieHandler.get(request.uri(), OkHeaders.toMultimap
(result.build().headers(), null)));
}
if (request.header(Network.USER_AGENT) == null) {
result.header(Network.USER_AGENT, Version.userAgent());
}
return result.build();
}
示例14: _getAsyn
import com.squareup.okhttp.Request; //导入依赖的package包/类
/**
* 同步的Get请求
*
* @param url
* @return Response
*/
private Response _getAsyn(String url) throws IOException {
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
Response execute = call.execute();
return execute;
}
示例15: post
import com.squareup.okhttp.Request; //导入依赖的package包/类
Call post(String mode, String username, String password, Callback callback) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("mode", mode)
.add("username", username + "@snu.in")
.add("password", password)
.build();
Request request = new Request.Builder()
.url("http://192.168.50.1/24online/servlet/E24onlineHTTPClient")
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}