Search in sources :

Example 66 with FormBody

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

the class testUASApi method getCompoData.

/**
 * 下拉接口
 */
public static void getCompoData(String caller, String field) {
    String url = baseurl + "mobile/common/getCombo.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("field", field).add("caller", caller).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) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 67 with FormBody

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

the class testPlatformApi method loginB2B.

/**
 * 登录 B2B
 * {"username":"18680669690","spaceId":"88745","password":"111111","appId":"b2b"}
 * @param url
 * @param username
 * @param password
 */
public static void loginB2B(String url, String username, String password) {
    RequestBody formBody = new FormBody.Builder().add("appId", "b2b").add("username", username).add("spaceId", "88745").add("password", password).build();
    Request request = new Request.Builder().url(url).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.println(url);
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            // OkhttpUtils.println("size:"+JSON.toJSONString(response.headers().size()));
            // OkhttpUtils.println("size
            // 4:"+JSON.toJSONString(response.headers().name(4)));
            // OkhttpUtils.println("size
            // 5:"+JSON.toJSONString(response.headers().name(5)));
            // //打印完整的cookie
            // OkhttpUtils.println("all
            // hearders:"+JSON.toJSONString(response.headers().toString()));
            // //打印完整的json格式数据
            // OkhttpUtils.println("all
            // hearders:"+JSON.toJSONString(response.headers().toMultimap()));
            // //打印多个key为 Set-Cookie的值
            // OkhttpUtils.println("set-cookie:"+JSON.toJSONString(response.headers("Set-Cookie")));
            // //打印多个key,多个key会自动放入数组里面
            // OkhttpUtils.println("set-cookie:"+JSON.toJSONString(response.header("Set-Cookie")));
            // OkhttpUtils.println("set-cookie
            // 5:"+JSON.toJSONString(response.headers().value(5)));
            // OkhttpUtils.println("set-cookie
            // 6:"+JSON.toJSONString(response.headers().value(6)));
            // OkhttpUtils.println(JSON.toJSONString(response));
            String json = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println("b2b登录:" + json);
            for (String iterable_element : response.headers("Set-Cookie")) {
                cookies = cookies + iterable_element + ";";
            }
            cookies = cookies.substring(0, cookies.length() - 1);
            OkhttpUtils.println("cookies:" + cookies);
            loginCall();
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.println("登录b2b,超时响应" + 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)

Example 68 with FormBody

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

the class OkhttpUtilsMain method sendSysResquest.

/**
 * Okhttp 异步请求
 */
public static void sendSysResquest() {
    RequestBody formBody = new FormBody.Builder().add("username", "123").add("password", "df13edafsdddsads").build();
    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();
    Request request = new Request.Builder().url("http://localhost:8080/spring-mvc-showcase/client/info").header("cookie", "JSESSIONID=EB36DE5E50E342D86C55DAE0CDDD4F6D").addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                String json = response.body().string();
                System.out.println(json);
            } else {
                System.out.println(JSON.toJSONString(response.code()));
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println(ExceptionUtils.printExceptionStack(e));
            if (e instanceof ConnectException) {
                System.out.println("服务器拒绝访问!");
            } else if (e instanceof SocketTimeoutException) {
                System.out.println("超时响应!");
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) SocketTimeoutException(java.net.SocketTimeoutException) RequestBody(okhttp3.RequestBody) ConnectException(java.net.ConnectException)

Example 69 with FormBody

use of okhttp3.FormBody in project Slide by ccrama.

the class UploadImgurAlbum method doInBackground.

@Override
protected String doInBackground(Uri... sub) {
    totalCount = sub.length;
    final OkHttpClient client = Reddit.client;
    String albumurl;
    {
        Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/album").post(new RequestBody() {

            @Override
            public MediaType contentType() {
                return null;
            }

            @Override
            public void writeTo(BufferedSink sink) {
            }
        }).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            JSONObject album = new JSONObject(response.body().string());
            albumurl = album.getJSONObject("data").getString("deletehash");
            finalUrl = "http://imgur.com/a/" + album.getJSONObject("data").getString("id");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    try {
        MultipartBody.Builder formBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (Uri uri : sub) {
            File bitmap = ImgurUtils.createFile(uri, c);
            formBodyBuilder.addFormDataPart("image", bitmap.getName(), RequestBody.create(MediaType.parse("image/*"), bitmap));
            formBodyBuilder.addFormDataPart("album", albumurl);
            MultipartBody formBody = formBodyBuilder.build();
            ProgressRequestBody body = new ProgressRequestBody(formBody, this::publishProgress);
            Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/image").post(body).build();
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) File(java.io.File) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody) RequestBody(okhttp3.RequestBody) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody)

Example 70 with FormBody

use of okhttp3.FormBody in project Slide by ccrama.

the class UploadImgur method doInBackground.

@Override
protected JSONObject doInBackground(Uri... sub) {
    File bitmap = ImgurUtils.createFile(sub[0], c);
    final OkHttpClient client = Reddit.client;
    try {
        RequestBody formBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("image", bitmap.getName(), RequestBody.create(MediaType.parse("image/*"), bitmap)).build();
        ProgressRequestBody body = new ProgressRequestBody(formBody, this::publishProgress);
        Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/image").post(body).build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);
        return new JSONObject(response.body().string());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody)

Aggregations

Request (okhttp3.Request)61 Response (okhttp3.Response)58 FormBody (okhttp3.FormBody)54 RequestBody (okhttp3.RequestBody)43 IOException (java.io.IOException)40 Call (okhttp3.Call)33 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)20 Map (java.util.Map)16 HttpUrl (okhttp3.HttpUrl)10 OkHttpClient (okhttp3.OkHttpClient)10 HashMap (java.util.HashMap)9 MultipartBody (okhttp3.MultipartBody)9 ArrayList (java.util.ArrayList)6 MediaType (okhttp3.MediaType)6 TypeToken (com.google.gson.reflect.TypeToken)4 File (java.io.File)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 List (java.util.List)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2