zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    java-反射
    java-异常
    Jmeter-逻辑控制器
    Jmeter-断言
    java-函数传参
    怎么测试一个杯子
    java+selenium3-元素定位
    java+selenium3-简介及环境搭建
    微软企业库5.0 使用举例
    数据库连接字符串ConnectionString 中的关键字值释义
  • 原文地址:https://www.cnblogs.com/juham/p/15239297.html
Copyright © 2011-2022 走看看