android 使用Http的Get方式讀取網絡數據

作爲移動平臺的應用,一定避免不了與網絡交換數據,不論是讀取網頁數據,還是調用API接口,都必須掌握Http通信技術

使用Get方式與網絡通信是最常見的Http通信,建立鏈接之後就可以通過輸入流讀取網絡數據。
代碼:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //異步加載
                new AsyncTask<String,Void,Void>(){

                    @Override
                    protected Void doInBackground(String... strings) {
                        try {
                            URL url=new URL(strings[0]);
                            URLConnection connection=url.openConnection();//獲取互聯網連接
                            InputStream is=connection.getInputStream();//獲取輸入流
                            InputStreamReader isr=new InputStreamReader(is,"utf-8");//字節轉字符,字符集是utf-8
                            BufferedReader bufferedReader=new BufferedReader(isr);//通過BufferedReader可以讀取一行字符串
                            String line;
                            while ((line=bufferedReader.readLine())!=null){
                                Log.i("輸出:",""+line);
                            }
                            bufferedReader.close();
                            isr.close();
                            is.close();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                    //使用api的數據接口
                }.execute(" http://fanyi.youdao.com/openapi.do?keyfrom=testdemoHttpGet&key=660196743&type=data&doctype=xml&version=1.1&q=good ");
            }
        });



    }
}

佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="custom.community.com.filedemo.MainActivity">

    <Button
        android:id="@+id/btn"

        android:text="讀取數據"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

AndroidManifest.xml配置網絡

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="custom.community.com.filedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>
  **<uses-permission android:name="android.permission.INTERNET"/>**
</manifest>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章