Search in sources :

Example 81 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project AndroidFastDevFrame by linglongxin24.

the class HttpRequest method getRequestBody.

private String getRequestBody(MultipartBody.Part part) {
    Class<?> personType = part.getClass();
    // 访问私有属性
    Field field = null;
    try {
        field = personType.getDeclaredField("body");
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    field.setAccessible(true);
    try {
        RequestBody requestBody = (RequestBody) field.get(part);
        MediaType contentType = requestBody.contentType();
        if (contentType.type().equals("multipart")) {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);
            Charset UTF8 = Charset.forName("UTF-8");
            Charset charset = contentType.charset(UTF8);
            return buffer.readString(charset);
        } else if (contentType.type().equals("image")) {
            return convertFileSize(requestBody.contentLength());
        // Class<?> requestBodyClass = requestBody.getClass();
        // 
        // //访问私有属性
        // Field fileRequestBodyClass = null;
        // try {
        // fileRequestBodyClass = requestBodyClass.getDeclaredField("file");
        // 
        // 
        // fileRequestBodyClass.setAccessible(true);
        // File file = (File) fileRequestBodyClass.get(requestBody);
        // Logger.d("file=" + file.getPath());
        // } catch (NoSuchFieldException e) {
        // e.printStackTrace();
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : Buffer(okio.Buffer) Field(java.lang.reflect.Field) MediaType(okhttp3.MediaType) Charset(java.nio.charset.Charset) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 82 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project NASABot by SniperNoob95.

the class DBClient method issuePutRequest.

public boolean issuePutRequest(String path, JSONObject payload) {
    try {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), payload.toString());
        Request request = new Request.Builder().url(url + path).put(requestBody).build();
        Response response = httpClient.newCall(request).execute();
        if (response.code() != 201) {
            response.close();
            return false;
        }
        response.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 83 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project NASABot by SniperNoob95.

the class DBClient method issuePostRequest.

public boolean issuePostRequest(String path, JSONObject payload) {
    try {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), payload.toString());
        Request request = new Request.Builder().url(url + path).post(requestBody).build();
        Response response = httpClient.newCall(request).execute();
        if (response.code() != 201) {
            response.close();
            return false;
        }
        response.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 84 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project NASABot by SniperNoob95.

the class TopGGClient method setStats.

private static void setStats(OkHttpClient httpClient, int serverCount) {
    try {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new JSONObject().put("server_count", serverCount).toString());
        Request request = new Request.Builder().url(url + "/stats").method("POST", requestBody).addHeader("Authorization", "Bearer " + token).build();
        Response response = httpClient.newCall(request).execute();
        response.close();
    } catch (Exception e) {
        ErrorLogging.handleError("TopGGClient", "setStats", "Cannot set stats.", e);
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 85 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project AndroidFastDevFrame by linglongxin24.

the class RequestParameter method getFilePartMap.

/**
 * 获取文件数组组装后的参数
 *
 * @param key   parameter_key
 * @param files 要上传的文件集合
 * @return
 */
public static Map<String, RequestBody> getFilePartMap(String key, List<File> files) {
    Map<String, RequestBody> paramsMap = new ArrayMap<>();
    for (int i = 0; i < files.size(); i++) {
        File file = files.get(i);
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
        paramsMap.put(key + "\"; filename=\"" + file.getName(), fileBody);
    }
    return paramsMap;
}
Also used : ArrayMap(android.support.v4.util.ArrayMap) File(java.io.File) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)1358 Request (okhttp3.Request)785 Response (okhttp3.Response)598 IOException (java.io.IOException)420 Test (org.junit.Test)235 OkHttpClient (okhttp3.OkHttpClient)216 MultipartBody (okhttp3.MultipartBody)213 MediaType (okhttp3.MediaType)204 Call (okhttp3.Call)198 JSONObject (org.json.JSONObject)183 ResponseBody (okhttp3.ResponseBody)177 Callback (okhttp3.Callback)115 FormBody (okhttp3.FormBody)106 Buffer (okio.Buffer)98 File (java.io.File)92 Map (java.util.Map)90 JsonObject (io.vertx.core.json.JsonObject)89 Headers (okhttp3.Headers)88 HashMap (java.util.HashMap)83 HttpUrl (okhttp3.HttpUrl)80