use of okhttp3.FormBody in project ForPDA by RadiationX.
the class Client method prepareRequest.
private Request.Builder prepareRequest(NetworkRequest request, ProgressListener uploadProgressListener) {
String url = request.getUrl();
if (request.getUrl().substring(0, 2).equals("//")) {
url = "https:".concat(request.getUrl());
}
Log.d(LOG_TAG, "Request url " + request.getUrl());
Request.Builder requestBuilder = new Request.Builder().url(url).header("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4").header("User-Agent", USER_AGENT);
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
Log.d(LOG_TAG, "Header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
requestBuilder.header(entry.getKey(), entry.getValue());
}
}
if (request.getFormHeaders() != null || request.getFile() != null) {
Log.d(LOG_TAG, "Multipart " + request.isMultipartForm());
if (request.getFormHeaders() != null) {
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
Log.d(LOG_TAG, "Form header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
}
}
if (request.getFile() != null) {
Log.d(LOG_TAG, "Form file " + request.getFile().toString());
}
if (!request.isMultipartForm()) {
if (request.getFormHeaders() != null) {
FormBody.Builder formBuilder = new FormBody.Builder();
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
formBuilder.add(entry.getKey(), entry.getValue());
if (request.getEncodedFormHeaders() != null && request.getEncodedFormHeaders().contains(entry.getKey())) {
formBuilder.addEncoded(entry.getKey(), entry.getValue());
} else {
formBuilder.add(entry.getKey(), entry.getValue());
}
}
FormBody formBody = formBuilder.build();
requestBuilder.post(formBody);
}
} else {
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (request.getFormHeaders() != null) {
for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
}
}
if (request.getFile() != null) {
MediaType type = MediaType.parse(request.getFile().getMimeType());
RequestBody requestBody = RequestBodyUtil.create(type, request.getFile().getFileStream());
multipartBuilder.addFormDataPart(request.getFile().getRequestName(), request.getFile().getFileName(), requestBody);
}
MultipartBody multipartBody = multipartBuilder.build();
if (uploadProgressListener == null) {
requestBuilder.post(multipartBody);
} else {
requestBuilder.post(new ProgressRequestBody(multipartBody, uploadProgressListener));
}
}
}
return requestBuilder;
}
use of okhttp3.FormBody in project Manhua by ag2s20150909.
the class API method search.
public static String search(String kw) {
// &pageSize=20&keyword
FormBody.Builder buider = new FormBody.Builder();
buider.add("pageSize", "20").add("keyword", kw).add("page", "1");
//
FormBody formBody = APIheper.getFormBuider(buider).build();
// String url=host + "/api/recom/index";
String url = host + "/api/book/search";
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();
}
}
use of okhttp3.FormBody in project Manhua by ag2s20150909.
the class API method getRecom.
public static String getRecom() {
// &pageSize=20&keyword
FormBody.Builder buider = new FormBody.Builder();
//
FormBody formBody = APIheper.getFormBuider(buider).build();
String url = host + "/api/recom/index";
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(4);
} else {
return "error:" + response.message() + " errorcode:" + response.code();
}
} catch (Exception e) {
return "error:" + e.getMessage();
}
}
use of okhttp3.FormBody in project Manhua by ag2s20150909.
the class API method getRanking.
public static String getRanking(String type, int page, int pageSize) {
// rankType=newOnline&page=1
FormBody.Builder buider = new FormBody.Builder();
buider.add("rankType", type);
buider.add("page", page + "");
buider.add("pageSize", pageSize + "");
//
FormBody formBody = APIheper.getFormBuider(buider).build();
String url = host + "/api/book/ranking";
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(4);
} else {
return "error:" + response.message() + " errorcode:" + response.code();
}
} catch (Exception e) {
return "error:" + e.getMessage();
}
}
use of okhttp3.FormBody in project Manhua by ag2s20150909.
the class API method search.
public static SearchBean search(String kw, int page, int pagesize) {
Gson gson = new Gson();
SearchBean result = new SearchBean();
FormBody.Builder buider = new FormBody.Builder();
buider.add("pageSize", pagesize + "").add("keyword", kw).add("page", page + "");
FormBody formBody = APIheper.getFormBuider(buider).build();
String url = host + "/api/book/search";
Request request = new Request.Builder().post(formBody).url(url).build();
try {
Response response = APP.getOkhttpClient().newCall(request).execute();
if (response.isSuccessful()) {
result = gson.fromJson(response.body().string(), SearchBean.class);
return result;
} else {
result.code = 1;
result.success = false;
result.message = "nessage:" + response.message() + " \ncode:" + response.code();
return result;
// return "error:" + response.message() + " errorcode:" + response.code();
}
} catch (Exception e) {
result.code = 1;
result.success = false;
result.message = e.getLocalizedMessage();
return result;
}
}
Aggregations