Search in sources :

Example 16 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method getChapters.

public static String getChapters(String bookid) {
    // &bid=320&sortType=ASC
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("sortType", "ASC").add("bid", bookid);
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    // String url=host + "/api/recom/index";
    String url = host + "/api/book/chapters";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString();
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 17 with FormBody

use of okhttp3.FormBody in project drill by apache.

the class SimpleHttp method buildPostBody.

/**
 * Accepts text from a post body in the format:<br>
 * {@code key1=value1}<br>
 * {@code key2=value2}
 * <p>
 * and creates the appropriate headers.
 *
 * @return FormBody.Builder The populated formbody builder
 */
private FormBody.Builder buildPostBody(String postBody) {
    FormBody.Builder formBodyBuilder = new FormBody.Builder();
    if (StringUtils.isEmpty(postBody)) {
        return formBodyBuilder;
    }
    final Pattern postBodyPattern = Pattern.compile("^.+=.+$");
    String[] lines = postBody.split("\\r?\\n");
    for (String line : lines) {
        // Otherwise ignore
        if (postBodyPattern.matcher(line).find()) {
            // Split into key/value
            String[] parts = line.split("=");
            formBodyBuilder.add(parts[0], parts[1]);
        }
    }
    return formBodyBuilder;
}
Also used : Pattern(java.util.regex.Pattern) Builder(okhttp3.OkHttpClient.Builder) FormBody(okhttp3.FormBody)

Example 18 with FormBody

use of okhttp3.FormBody 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 19 with FormBody

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

the class testUASApi method selectCardLog.

public static void selectCardLog(String date, String method) {
    // {master=USOFTSYS, emcode=U0316, pageSize=100, sessionUser=U0316,
    // condition=cl_emcode='U0316' and to_char(cl_time,'yyyy-MM-dd')='2017-03-03',
    // sessionId=29DB60DE6E40D859B9169FE5013A8520, caller=CardLog, page=1, currentMaster=USOFTSYS}
    String url = baseurl + "/mobile/oa/workdata.action";
    RequestBody formBody = new FormBody.Builder().add("currentMaster", master).add("master", master).add("emcode", emcode).add("condition", "cl_emcode='U0316' and to_char(cl_time,'yyyy-MM-dd')='" + date + "'").add("caller", "CardLog").add("page", "1").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();
    try {
        Response response = OkhttpUtils.client.newCall(request).execute();
        OkhttpUtils.println(response.body().string());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
// 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);
// 
// 
// 
// }
// 
// @Override
// public void onFailure(Call call, IOException e) {
// OkhttpUtils.onFailurePrintln(e);
// }
// });
// 
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 20 with FormBody

use of okhttp3.FormBody 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

Request (okhttp3.Request)61 Response (okhttp3.Response)58 FormBody (okhttp3.FormBody)53 RequestBody (okhttp3.RequestBody)43 IOException (java.io.IOException)39 Call (okhttp3.Call)32 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)20 Map (java.util.Map)15 HttpUrl (okhttp3.HttpUrl)10 OkHttpClient (okhttp3.OkHttpClient)10 MultipartBody (okhttp3.MultipartBody)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)6 MediaType (okhttp3.MediaType)6 TypeToken (com.google.gson.reflect.TypeToken)4 File (java.io.File)3 DataInputStream (java.io.DataInputStream)2 List (java.util.List)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2