Search in sources :

Example 71 with Callback

use of zipkin2.Callback in project PLDroidPlayer by pili-engineering.

the class ModelFactory method createUpgradeInfoByURL.

public static void createUpgradeInfoByURL(String url, final OnResultListener listener) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            listener.onFailure();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            UpgradeInfo upgradeInfo = new UpgradeInfo();
            try {
                JSONObject jsonObject = new JSONObject(response.body().string());
                upgradeInfo.setAppID(jsonObject.optString("AppId"));
                upgradeInfo.setVersion(jsonObject.optString("Version"));
                upgradeInfo.setDescription(jsonObject.optString("Description"));
                upgradeInfo.setDownloadURL(jsonObject.optString("DownloadURL"));
                upgradeInfo.setCreateTime(jsonObject.optString("CreateAt"));
                listener.onSuccess(response.code(), upgradeInfo);
            } catch (JSONException e) {
                e.printStackTrace();
                listener.onFailure();
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 72 with Callback

use of zipkin2.Callback in project Gradle-demo by Arisono.

the class OkhttpUtils method sendPostHttp.

/**
 * post http
 * @param url
 * @param params
 * @param tag
 */
public static void sendPostHttp(String url, Map<String, Object> params, String cookies, String tag) {
    Builder paramBuilder = new FormBody.Builder();
    if (!params.isEmpty()) {
        Iterator<Map.Entry<String, Object>> entries = params.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            paramBuilder.add(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
        OkhttpUtils.println(tag + ":" + url);
        RequestBody formBody = paramBuilder.build();
        Request request = new Request.Builder().url(url).addHeader("content-type", "text/html;charset:utf-8").addHeader("Cookie", cookies).post(formBody).build();
        OkhttpUtils.client.newCall(request).enqueue(new Callback() {

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String requestJson = OkhttpUtils.getResponseString(response);
                RxBus.getInstance().send(tag + ":" + requestJson);
            }

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

Example 73 with Callback

use of zipkin2.Callback in project Gradle-demo by Arisono.

the class OkhttpUtils method sendGetHttp.

/**
 * get http
 * @param url
 * @param tag
 */
public static void sendGetHttp(String url, Map<String, Object> params, String cookies, String tag) {
    StringBuilder buf = new StringBuilder(url);
    if (params != null) {
        if (!params.isEmpty()) {
            if (url.indexOf("?") == -1)
                buf.append("?");
            else if (!url.endsWith("&"))
                buf.append("&");
            Iterator<Map.Entry<String, Object>> entries = params.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, Object> entry = entries.next();
                buf.append(String.valueOf(entry.getKey())).append("=").append(String.valueOf(entry.getValue())).append("&");
            }
            buf.deleteCharAt(buf.length() - 1);
        }
    }
    Request request = new Request.Builder().url(buf.toString()).addHeader("content-type", "text/html;charset:utf-8").addHeader("Cookie", "12").build();
    OkhttpUtils.println(tag + ":" + buf.toString());
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String requestJson;
            requestJson = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(requestJson);
            RxBus.getInstance().send(tag + ":" + requestJson);
        }

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

Example 74 with Callback

use of zipkin2.Callback in project Gradle-demo by Arisono.

the class OkhttpUtils method uploadFile.

/**
 * 多文件+参数 上传功能
 * @param url
 * @param params
 * @param filePaths
 */
public static void uploadFile(String url, Map<String, Object> params) {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    // 追加参数
    for (String key : params.keySet()) {
        Object object = params.get(key);
        if (!(object instanceof File)) {
            builder.addFormDataPart(key, object.toString());
        } else {
            File file = (File) object;
            // 其中参数“file”和服务器接收的参数 一一对应,保证多文件上传唯一key不变
            builder.addFormDataPart("file", file.getName(), RequestBody.create(null, file));
        }
    }
    // 创建RequestBody
    RequestBody body = builder.build();
    Request request = new Request.Builder().url(url).post(body).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String requestJson = OkhttpUtils.getResponseString(response);
            RxBus.getInstance().send("uploadFile" + ":" + requestJson);
        }

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

Example 75 with Callback

use of zipkin2.Callback in project Gradle-demo by Arisono.

the class testUASApi method addWorkReport.

/**
 * 添加日报
 */
public static void addWorkReport() {
    String url = baseurl + "mobile/addWorkReport.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("formStore", "{\"wd_experience\":\"\",\"wd_comment\":\"13.37一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八九\\n一二三四五六七八九十一二三四五六七八\\n一二三\",\"wd_empcode\":\"U0747\",\"wd_plan\":\"\"}").add("caller", "WorkDaily").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)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14