github:
https://github.com/anymyna/ui
1、get 请求
final Request request=new Request.Builder()
.get()
.tag(this)
.url("https://api.github.com/events")
.build();
new Thread(new Runnable() {
@Override
public void run() {
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
Log.i(Tag,"response:" + response.body().string());
} else {
throw new IOException("Unexpected code " + response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
2、Post 请求
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
//.add("name", name).add("pwd", pwd)
.build();
Request request = new Request.Builder()
.post(formBody)
.url("http://httpbin.org/post")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(Tag,"response:" + response.body().string());
}
});
3、Post 以表单的形式来上传数组
FormBody.Builder builder = new FormBody.Builder();
builder.add("cate[]",arrayTest[0]);
builder.add("cate[]",arrayTest[1]);