android 使用Http的Get方式讀取網路資料

來源:互聯網
上載者:User

標籤:

作為移動平台的應用,一定避免不了與網路交換資料,不論是讀取網頁資料,還是調用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>

android 使用Http的Get方式讀取網路資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.