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);
}
});
}
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);
}
});
}
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("超时响应!");
}
}
});
}
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;
}
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;
}
Aggregations