• 07-网络与通信-02-Android中基于HTTP的通信技术




    02-Android中基于HTTP的通信技术

      MainActivity.java:


    package com.jikexueyuan.httpget;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    new AsyncTask<String, Void, Void>(){
    
                        @Override
                        protected Void doInBackground(String... params) {
                            try {
                                URL url = new URL(params[0]);
                                URLConnection connection = url.openConnection();
                                InputStream is = connection.getInputStream();
                                InputStreamReader isr = new InputStreamReader(is,"utf-8");
                                BufferedReader br = new BufferedReader(isr);
                                String line;
                                while((line = br.readLine()) != null){
                                    System.out.println(line);
                                }
                                br.close();
                                isr.close();
                                is.close();
                            } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            
                            return null;
                        }
                        
                    }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=testHttpGet&key=850021564&type=data&doctype=xml&version=1.1&q=good");
                }
            });
        }
    
    }

    activity_main.xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.jikexueyuan.httpget.MainActivity"
        tools:ignore="MergeRootFrame" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="读取数据" />
    
    </FrameLayout>
  • 相关阅读:
    Find the capitals
    Area of a Circle
    SequenceSum
    使用Eclipse设定Android开发环境
    hdu 1290 竭诚为杭州电礼物50周年
    oracle connect by 说明
    CSS截取字符串,额外的文本显示以省略号
    动态规划——背包问题
    【MySQL笔记】mysql来源安装/配置步骤和支持中国gbk/gb2312编码配置
    博客搬到了http://xianglong.me
  • 原文地址:https://www.cnblogs.com/juham/p/15239297.html
Copyright © 2020-2023  润新知