use of com.reprezen.kaizen.oasparser.model3.MediaType in project student-manager by SYYANI.
the class LoginActivity method login.
private void login(String username, String password) throws IOException, JSONException {
// Log.d(TAG, "login: "+user_kind);
MediaType JSON = MediaType.parse("application/json;charset=utf-8");
JSONObject json = new JSONObject();
if (user_kind == "teacher") {
json.put("tid", username);
} else if (user_kind == "student") {
json.put("sid", username);
}
json.put("password", password);
String url = "http://101.35.20.64:10086/" + user_kind + "/login";
new Thread() {
@Override
public void run() {
super.run();
Message message = handler.obtainMessage();
Bundle bundle = new Bundle();
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(JSON, json.toString());
Request request = new Request.Builder().url(url).method("POST", body).addHeader("Content-Type", "application/json;charset=utf-8").build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
// Log.d(TAG, "run: "+response.body().string());
Boolean login_true = Boolean.parseBoolean(response.body().string());
if (login_true) {
Log.d(TAG, "UserLogin: success");
message.what = RequestConstant.REQUEST_SUCCESS;
bundle.putString("UserKind", user_kind);
load(username);
} else {
message.what = RequestConstant.REQUEST_FAILURE;
}
}
} catch (IOException e) {
e.printStackTrace();
}
message.setData(bundle);
handler.sendMessage(message);
}
}.start();
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project FastAndroid by jerryliuinfo.
the class HttpLogInterceptor method intercept.
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
if (hasRequestBody) {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
NLog.d(TAG, "发送请求 %s on %s %n%s%s", request.url(), request.method(), request.headers(), this.isPlaintext(buffer) ? buffer.readString(charset) : "");
} else {
NLog.d(TAG, "发送请求 %s on %s %n%s\"", request.url(), request.method(), request.headers());
}
Response response = chain.proceed(request);
long t2 = System.nanoTime();
ResponseBody responseBody = response.peekBody(1048576L);
NLog.d(TAG, "接收响应(%.1fms): [%s] %n%s%s", (double) (t2 - t1) / 1000000.0D, response.request().url(), response.headers(), responseBody.string());
return response;
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project habpanelviewer by vbier.
the class RestClient method putRequest.
private void putRequest(ItemModification item) {
try {
OkHttpClient client = OkHttpClientFactory.getInstance().create();
MediaType PLAIN = MediaType.get("text/plain; charset=utf-8");
RequestBody body = RequestBody.create(PLAIN, item.mItemState);
Request request = new Request.Builder().url(item.mServerURL + "/rest/items/" + item.mItemName + "/state").put(body).build();
try (Response response = client.newCall(request).execute()) {
try (ResponseBody responseBody = response.body()) {
if (responseBody != null) {
Log.v(TAG, "set " + item.mItemName + " request response: " + responseBody.string() + "(" + response.code() + ")");
}
}
}
} catch (IOException e) {
Log.e(TAG, "Failed to set state for item " + item.mItemName, e);
}
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project teku by ConsenSys.
the class OkHttpStreamClient method createRequest.
@Override
protected Call createRequest(final StreamRequestMessage requestMessage) {
final UpnpRequest.Method method = requestMessage.getOperation().getMethod();
final RequestBody body;
if (method == UpnpRequest.Method.POST || method == UpnpRequest.Method.NOTIFY) {
final MediaType mediaType = MediaType.get(requestMessage.getContentTypeHeader().getString());
if (requestMessage.getBodyType() == UpnpMessage.BodyType.STRING) {
body = RequestBody.create(requestMessage.getBodyString(), mediaType);
} else {
body = RequestBody.create(requestMessage.getBodyBytes(), mediaType);
}
} else {
body = null;
}
final Headers.Builder headersBuilder = new Headers.Builder();
requestMessage.getHeaders().forEach((k, v) -> v.forEach(s -> headersBuilder.add(k, s)));
final Request request = new Request.Builder().url(requestMessage.getUri().toString()).method(requestMessage.getOperation().getHttpMethodName(), body).headers(headersBuilder.build()).build();
return client.newCall(request);
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project DataSpaceConnector by eclipse-dataspaceconnector.
the class HttpDataSourceFactory method createDataSource.
private Result<HttpDataSource> createDataSource(DataFlowRequest request) {
var dataAddress = request.getSourceDataAddress();
var endpoint = dataAddress.getProperty(ENDPOINT);
if (StringUtils.isNullOrBlank(endpoint)) {
return Result.failure("HTTP data source endpoint not provided for request: " + request.getId());
}
var method = request.getProperties().get(METHOD);
if (StringUtils.isNullOrBlank(method)) {
return Result.failure("Method not provided for request: " + request.getId());
}
var name = dataAddress.getProperty(NAME);
var mediaTypeStr = request.getProperties().get(MEDIA_TYPE);
MediaType mediaType = null;
if (mediaTypeStr != null) {
mediaType = MediaType.parse(mediaTypeStr);
if (mediaType == null) {
return Result.failure("Unhandled media type: " + mediaTypeStr);
}
}
var builder = HttpDataSource.Builder.newInstance().httpClient(httpClient).requestId(request.getId()).sourceUrl(endpoint).name(name).method(method).retryPolicy(retryPolicy).monitor(monitor);
Optional.ofNullable(mediaType).ifPresent(mt -> builder.requestBody(mt, request.getProperties().get(BODY)));
Optional.ofNullable(dataAddress.getProperty(AUTHENTICATION_KEY)).ifPresent(s -> builder.header(s, dataAddress.getProperty(AUTHENTICATION_CODE)));
Optional.ofNullable(request.getProperties().get(QUERY_PARAMS)).ifPresent(builder::queryParams);
try {
return Result.success(builder.build());
} catch (Exception e) {
return Result.failure("Failed to build HttpDataSource: " + e.getMessage());
}
}
Aggregations