zoukankan      html  css  js  c++  java
  • OkHttp

    创建项目:okhttpcho1

             1、到官网下载两个jar文件:

                                 

             2、activity_main.xml文件:

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Button" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginRight="35dp"
            android:layout_marginTop="44dp"
            android:src="@drawable/ic_launcher" />
    

             添加一个ImageView图片控件和一个Button按钮控件。

           3、MainActivity.java文件:       

                          我们的服务端数据还是使用上一章的text1.jsp文件。

    public class MainActivity extends Activity {
        private Button btn;
        private ImageView img;
        private String url1_json = "http://android2017.duapp.com/test1.jsp";
        private String url2_img = "http://www.admin10000.com/UploadFiles/Document/201307/14/20130714201135796898.JPG";
        private Bitmap bitmap;
        private String json_str;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn=(Button)findViewById(R.id.button1);
            img=(ImageView)findViewById(R.id.imageView1);
            btn.setOnClickListener(new OnClickListener(){
    
                @Override
                public void onClick(View v) {
                    //创建一个OkhttpClient对象
                    OkHttpClient http=new OkHttpClient();
                    //创建一个Request对象获取
                    Request rqust=new Request.Builder().url(url1_json).build();
                    //希望得到一个response
                    //为了防止卡顿,把获取response调到后台进程去
                    //调度
                    Call call=http.newCall(rqust);
                    //调用enqueue异步请求,将call加入调度队列,等待任务完成在Callback中得到结果。
                    call.enqueue(new Callback(){
    
                        
                        //失败
                        @Override
                        public void onFailure(Request argo, IOException arg1) {
                            // TODO Auto-generated method stub
                            
                        }
                       //成功
                        @Override
                        public void onResponse(Response response) throws IOException {
                            // TODO Auto-generated method stub
                            //读取文本
                            json_str = response.body().string();
                            //读取二进制(图片)
                        //    bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                            //runOnUiThread调用主线程,通过Runnable接口的run方法进行运行
                            runOnUiThread(new Runnable(){
    
                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(MainActivity.this, json_str, 500).show();
                                //    img.setImageBitmap(bitmap);
                                }});
                        }});
                }});
        }
    
    
       
    }

    1、首先我们通过R获取到控件id,通过setOnClickListener方法给button添加监听实现onClick单机事件。定义一个Bitmap类型变量将图片保存到内存中。

    2、定义ulr,通过Request向服务端发起请求。

    3、通过okHttpConntion的newCall()方法将线程调到后台去运行,防止读取数据时卡住。

    4、通过Call的异步方法enqueue将call添加到调度队列中。

    5、异步方法的onResponse回调的参数是response,一般情况下,我们希望获得返回的字符串,可以通过response.body().string()获取;如果希望获得返回的二进制(图片),则调用BitmapFactory.decodeStream(response.body().byteStream())读取内存图片,如果你想拿到返回的inputStream,则调用response.body().byteStream()。

    6、在获取到数据后我们通过runOnUiThread调用主线程,并且通过Runnable接口的run方法进行运行,对UI界面进行操作。

    效果图:

                                 数据:                                                                        

                   

                                                                       

                                 图片:

                                                                                                                

  • 相关阅读:
    字符串哈希
    codeforces#766 D. Mahmoud and a Dictionary (并查集)
    莫比乌斯反演模板
    马拉车模板
    codeforces#580 D. Kefa and Dishes(状压dp)
    1076E
    448C
    543A
    295B
    poj3974 Palindrome
  • 原文地址:https://www.cnblogs.com/wdht/p/6149551.html
Copyright © 2011-2022 走看看