Search in sources :

Example 66 with RequestBody

use of okhttp3.RequestBody in project Tusky by Vavassor.

the class ComposeActivity method uploadMedia.

private void uploadMedia(final QueuedMedia item) {
    item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
    final String mimeType = getContentResolver().getType(item.uri);
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String fileExtension = map.getExtensionFromMimeType(mimeType);
    final String filename = String.format("%s_%s_%s.%s", getString(R.string.app_name), String.valueOf(new Date().getTime()), randomAlphanumericString(10), fileExtension);
    byte[] content = item.content;
    if (content == null) {
        InputStream stream;
        try {
            stream = getContentResolver().openInputStream(item.uri);
        } catch (FileNotFoundException e) {
            Log.d(TAG, Log.getStackTraceString(e));
            return;
        }
        content = inputStreamGetBytes(stream);
        IOUtils.closeQuietly(stream);
        if (content == null) {
            return;
        }
    }
    RequestBody requestFile = RequestBody.create(MediaType.parse(mimeType), content);
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestFile);
    item.uploadRequest = mastodonAPI.uploadMedia(body);
    item.uploadRequest.enqueue(new Callback<Media>() {

        @Override
        public void onResponse(Call<Media> call, retrofit2.Response<Media> response) {
            if (response.isSuccessful()) {
                onUploadSuccess(item, response.body());
            } else {
                Log.d(TAG, "Upload request failed. " + response.message());
                onUploadFailure(item, call.isCanceled());
            }
        }

        @Override
        public void onFailure(Call<Media> call, Throwable t) {
            Log.d(TAG, t.getMessage());
            onUploadFailure(item, false);
        }
    });
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Media(com.keylesspalace.tusky.entity.Media) StringUtils.randomAlphanumericString(com.keylesspalace.tusky.util.StringUtils.randomAlphanumericString) MimeTypeMap(android.webkit.MimeTypeMap) Date(java.util.Date) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 67 with RequestBody

use of okhttp3.RequestBody in project yyl_example by Relucent.

the class TraceInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    System.out.println("/==========TraceInterceptor==========");
    System.out.println("\nRequest:");
    System.out.println(request.method());
    System.out.println(request.url());
    System.out.println(request.headers());
    RequestBody requestBody = request.body();
    if (requestBody != null) {
        try (Buffer buffer = new Buffer()) {
            requestBody.writeTo(buffer);
            String content = buffer.readUtf8();
            System.out.println(content);
            requestBody = RequestBody.create(requestBody.contentType(), content);
        }
    }
    request = request.newBuilder().method(request.method(), requestBody).build();
    Response response = chain.proceed(request);
    MediaType mediaType = response.body().contentType();
    String content = response.body().string();
    System.out.println("\nResponse:");
    System.out.println(response.headers());
    System.out.println(content);
    System.out.println("==========TraceInterceptor==========/\n\n\n\n\n");
    return response.newBuilder().body(ResponseBody.create(mediaType, content)).build();
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 68 with RequestBody

use of okhttp3.RequestBody in project Gradle-demo by Arisono.

the class testUASApi method saveFormData.

/**
	 * 动态表单保存 接口
	 */
public static void saveFormData() {
    String url = baseurl + "/mobile/oa/commonSaveAndSubmit.action";
    OkhttpUtils.println(url);
    RequestBody formBody = new FormBody.Builder().add("master", master).add("gridStore", "null").add("formStore", "{\"va_holidaytype\":\"按小时\",\"va_vacationtype\":\"事假\",\"va_status\":\"在录入\",\"va_emcode\":\"sunquan\",\"va_emname\":\"龚鹏明\",\"va_department\":\"测试\",\"va_position\":\"测试\",\"va_mankind\":\"副总及以上\",\"va_alldays\":\"12\",\"va_alltimes\":\"25\",\"va_startime\":\"2016-11-28 13:39:00\",\"va_remark\":\"测试\",\"va_recordor\":\"刘佳\",\"va_date\":\"2016-11-28 13:39:00\",\"va_endtime\":\"2016-12-28 13:39:00\"}").add("caller", "Ask4Leave").add("sessionId", sessionId).build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            OkhttpUtils.println(OkhttpUtils.getResponseString(response));
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 69 with RequestBody

use of okhttp3.RequestBody in project Gradle-demo by Arisono.

the class testUASApi method getDBFindData.

/**
	 * @category 多选接口
	 */
public static void getDBFindData() {
    String url = baseurl + "common/dbfind.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("which", "form").add("pageSize", "5").add("condition", "1=1").add("field", "va_emname").add("caller", "Ask4Leave").add("sessionId", sessionId).add("page", "1").build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String json = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(json);
            String dataStr = JSON.parseObject(json).getString("data");
            // values
            JSONArray datas = JSON.parseArray(dataStr);
            System.out.println(datas.getJSONObject(0).getString("em_name"));
            JSONArray dbfinds = JSON.parseObject(json).getJSONArray("dbfinds");
            String fieldkey = "";
            for (int i = 0; i < dbfinds.size(); i++) {
                JSONObject item = dbfinds.getJSONObject(i);
                String key = item.getString("field");
                if (key.equals("va_emname")) {
                    System.out.println(item.getString("dbGridField"));
                    fieldkey = item.getString("dbGridField");
                }
            }
            System.out.println("url:" + url);
            System.out.println("master:" + master);
            System.out.println("emcode:" + emcode);
            for (int i = 0; i < datas.size(); i++) {
                System.out.println("value" + i + ":" + datas.getJSONObject(i).getString(fieldkey));
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Call(okhttp3.Call) FormBody(okhttp3.FormBody) Request(okhttp3.Request) JSONArray(com.alibaba.fastjson.JSONArray) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(com.alibaba.fastjson.JSONObject) RequestBody(okhttp3.RequestBody)

Example 70 with RequestBody

use of okhttp3.RequestBody in project Gradle-demo by Arisono.

the class testUASApi method getStringCode.

public static void getStringCode(String method) {
    //master=USOFTSYS, sessionUser=U0316, sessionId=29DB60DE6E40D859B9169FE5013A8520, caller=CardLog, type=2
    String url = baseurl + "common/getCodeString.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("type", "2").add("caller", "CardLog").add("sessionId", sessionId).build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String result = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(result, OkhttpUtils.typeMiddle, method);
            code = JSON.parseObject(result).getString("code");
            saveCardLog("12.12", code, "saveCardLog");
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11