1 首先需要创建一个OKHttpClient
可以直接new一个
[java] view plain copy
- OkHttpClient client = new OkHttpClient()
更多的是用builder构造一个 (那个addInterceptor方法是添加拦截器 , 可以不写,具体的baidu, bing, sogou)
[java] view plain copy
- private void buildHttpClient(){
- this.client = new OkHttpClient.Builder()
- .addInterceptor(new Interceptor() {
- public Response intercept(Chain chain) throws IOException {
- Request request = chain.request();
- Response response = chain.proceed(request);
- return response;
- }
- })
- .connectTimeout(4000, TimeUnit.MILLISECONDS)
- .readTimeout(4000,TimeUnit.MILLISECONDS)
- .writeTimeout(4000, TimeUnit.MILLISECONDS)
- .build();
- }
2 GET 请求
[java] view plain copy
- <strong> </strong>private void get(){
- /* 如果需要参数 , 在url后边拼接 : ?a=aaa&b=bbb..... */
- Request request = new Request.Builder().url("http://192.168.10.117:8080/test").build();
- client.newCall(request).enqueue(new Callback() {
- public void onResponse(Call call, final Response response) throws IOException {
- final String result = response.body().string();
- final boolean ok = response.isSuccessful();
- runOnUiThread(new Runnable() {
- public void run(){
- Toast.makeText(OKHttpActivity.this, result, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void onFailure(Call call, IOException e) {
- runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(OKHttpActivity.this, "error", Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
- }
3 POST 请求
[java] view plain copy
- private void post(){
- FormBody.Builder builder = new FormBody.Builder();
- /* 添加两个参数 */
- builder.add("p","我勒个去").add("a","hello");
- FormBody body = builder.build();
- Request request = new Request.Builder().url("http://192.168.10.117:8080/test").post(body).build();
- /* 下边和get一样了 */
- client.newCall(request).enqueue(new Callback() {
- public void onResponse(Call call, Response response) throws IOException {
- final String bodyStr = response.body().string();
- final boolean ok = response.isSuccessful();
- runOnUiThread(new Runnable() {
- public void run() {
- if(ok){
- Toast.makeText(OKHttpActivity.this, bodyStr, Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(OKHttpActivity.this, "server error : " + bodyStr, Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void onFailure(Call call,final IOException e) {
- runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(OKHttpActivity.this, "error : "+e.toString(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
- }
4 多文件上传
- private void upFile(){
- /* 第一个要上传的file */
- File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg");
- RequestBody fileBody1 = RequestBody.create(MediaType.parse("application/octet-stream") , file1);
- String file1Name = "testFile1.txt";
- /* 第二个要上传的文件,这里偷懒了,和file1用的一个图片 */
- File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg");
- RequestBody fileBody2 = RequestBody.create(MediaType.parse("application/octet-stream") , file2);
- String file2Name = "testFile2.txt";
- /* form的分割线,自己定义 */
- String boundary = "xx--------------------------------------------------------------xx";
- MultipartBody mBody = new MultipartBody.Builder(boundary).setType(MultipartBody.FORM)
- /* 上传一个普通的String参数 , key 叫 "p" */
- .addFormDataPart("p" , "你大爷666")
- /* 底下是上传了两个文件 */
- .addFormDataPart("file" , file1Name , fileBody1)
- .addFormDataPart("file" , file2Name , fileBody2)
- .build();
- /* 下边的就和post一样了 */
- Request request = new Request.Builder().url("http://192.168.10.117:8080/test").post(mBody).build();
- client.newCall(request).enqueue(new Callback() {
- public void onResponse(Call call, Response response) throws IOException {
- final String bodyStr = response.body().string();
- final boolean ok = response.isSuccessful();
- runOnUiThread(new Runnable() {
- public void run() {
- if(ok){
- Toast.makeText(OKHttpActivity.this, bodyStr, Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(OKHttpActivity.this, "server error : " + bodyStr, Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void onFailure(Call call, final IOException e) {
- runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(OKHttpActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
- }
- 获取链接
- X
- 电子邮件
- 其他应用
- 获取链接
- X
- 电子邮件
- 其他应用
评论
发表评论