Search in sources :

Example 76 with MediaType

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Message(android.os.Message) Bundle(android.os.Bundle) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 77 with MediaType

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;
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) Request(okhttp3.Request) Charset(java.nio.charset.Charset) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with MediaType

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);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody)

Example 79 with MediaType

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);
}
Also used : Request(okhttp3.Request) Callable(java.util.concurrent.Callable) AbstractStreamClient(org.jupnp.transport.spi.AbstractStreamClient) Headers(okhttp3.Headers) UpnpRequest(org.jupnp.model.message.UpnpRequest) UpnpResponse(org.jupnp.model.message.UpnpResponse) RequestBody(okhttp3.RequestBody) OkHttpClient(okhttp3.OkHttpClient) StreamResponseMessage(org.jupnp.model.message.StreamResponseMessage) StreamClientConfigurationImpl(org.jupnp.transport.impl.jetty.StreamClientConfigurationImpl) Response(okhttp3.Response) Call(okhttp3.Call) UpnpMessage(org.jupnp.model.message.UpnpMessage) StreamRequestMessage(org.jupnp.model.message.StreamRequestMessage) MediaType(okhttp3.MediaType) UpnpHeaders(org.jupnp.model.message.UpnpHeaders) UpnpRequest(org.jupnp.model.message.UpnpRequest) Headers(okhttp3.Headers) UpnpHeaders(org.jupnp.model.message.UpnpHeaders) Request(okhttp3.Request) UpnpRequest(org.jupnp.model.message.UpnpRequest) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 80 with MediaType

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());
    }
}
Also used : MediaType(okhttp3.MediaType) EdcException(org.eclipse.dataspaceconnector.spi.EdcException)

Aggregations

MediaType (okhttp3.MediaType)297 RequestBody (okhttp3.RequestBody)186 Request (okhttp3.Request)179 Response (okhttp3.Response)158 IOException (java.io.IOException)137 ResponseBody (okhttp3.ResponseBody)71 OkHttpClient (okhttp3.OkHttpClient)68 Charset (java.nio.charset.Charset)50 Buffer (okio.Buffer)50 Headers (okhttp3.Headers)48 JSONObject (org.json.JSONObject)38 BufferedSource (okio.BufferedSource)36 MultipartBody (okhttp3.MultipartBody)34 HttpUrl (okhttp3.HttpUrl)30 Map (java.util.Map)24 BufferedSink (okio.BufferedSink)23 File (java.io.File)22 InputStream (java.io.InputStream)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21