Chinaunix首页 | 论坛 | 博客
  • 博客访问: 280698
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Java

2017-02-05 12:25:14


用到的第三方包 OkHTTP

     Github地址:
注意:
        这里涉及到了网络操作,所以一定记得加上相关的权限:
  
  1. <uses-permission android:name="android.permission.INTERNET" />

一、GET请求:
    
  1. package barneyx.com.okhttpdemo;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.TextView;

  6. import org.w3c.dom.Text;

  7. import java.io.IOException;

  8. import okhttp3.Call;
  9. import okhttp3.Callback;
  10. import okhttp3.OkHttpClient;
  11. import okhttp3.Request;
  12. import okhttp3.Response;

  13. public class MainActivity extends AppCompatActivity {

  14.     private String mBaseUrl = "";
  15.     private TextView tv;

  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         tv = (TextView)findViewById(R.id.t_view);
  21.     }

  22.     public void doGet(View view) throws IOException{
  23.         //1.拿到okHttpClient对象
  24.         OkHttpClient okHttpClient = new OkHttpClient();

  25.         //2.构造Reqeust
  26.         Request.Builder builder = new Request.Builder();
  27.         Request request =builder.get().url(
  28.                 mBaseUrl+"user!login?username=bai&password=xiaobai"
  29.         ).build();

  30.         //3.将Request封装为Call
  31.         Call call = okHttpClient.newCall(request);
  32.         //Response response = call.execute();

  33.         //4.执行Call
  34.         call.enqueue(new Callback() {
  35.             @Override
  36.             public void onFailure(Call call, IOException e) {
  37.                 L.e("onFailure"+e.getMessage());
  38.                 e.printStackTrace();
  39.             }

  40.             @Override
  41.             public void onResponse(Call call, Response response) throws IOException {
  42.                 L.e("OnResponse:");
  43.                final String res = response.body().string();
  44.                 L.e(res);
  45.                 runOnUiThread(new Runnable() {
  46.                     @Override
  47.                     public void run() {
  48.                         tv.setText(res);
  49.                     }
  50.                 });


  51.             }
  52.         });
  53.     }
  54. }
activity_main.xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     xmlns:tools=""
  4.     android:id="@+id/activity_main"
  5.     android:orientation="vertical"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context="barneyx.com.okhttpdemo.MainActivity">
  9.     <Button
  10.         android:text="Get"
  11.         android:onClick="doGet"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content" />
  14.     <TextView
  15.         android:id="@+id/t_view"
  16.         android:text="hello world"
  17.         android:gravity="center"
  18.         android:paddingTop="10dp"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="wrap_content" />




  21. </LinearLayout>

阅读(1046) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

fjh1234562019-10-14 16:32:55

大撒大撒

fjh1234562019-10-14 16:32:52

大撒大撒