Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android利用Get、Post 獲取網絡數據

Android利用Get、Post 獲取網絡數據

編輯:關於Android編程

首先是Get:

布局很簡單,就一個按鈕,主要看一下MainActivity吧:

package com.francis.httpget;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

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;


public class MyActivity extends Activity {

    BufferedReader bufferedReader;
    InputStreamReader inputStreamReader;
    InputStream inputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask() {
                    @Override
                    protected Void doInBackground(String... params) {
                        try {
                            URL url = new URL(params[0]);
                            URLConnection connection = url.openConnection();

                            inputStream = connection.getInputStream();
                            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                            bufferedReader = new BufferedReader(inputStreamReader);

                            String line;
                            while (true) {
                                if ((line = bufferedReader.readLine()) != null) {
                                    System.out.println(line);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                bufferedReader.close();
                                inputStreamReader.close();
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        return null;
                    }
                }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=這裡是有道翻譯API");
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

url為有道翻譯提供的API,非常方便。控制台輸出如圖:

\


接著是Post:

很簡單,這裡只給出提交post請求的Button的點擊事件:

和Get相比有2點需要注意:

1.url與參數是分開的;<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Mi5jb25uZWN0aW9uLnNldERvSW5wdXQodHJ1ZSk7PGJyPgogICBjb25uZWN0aW9uLnNldERvT3V0cHV0KHRydWUpOzxicj4KICAgY29ubmVjdGlvbi5zZXRSZXF1ZXN0TWV0aG9kKA=="POST");

// Post
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask() {
                    @Override
                    protected Void doInBackground(String... params) {
                        try {
                            URL url = new URL(params[0]);
                            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                            connection.setDoInput(true);
                            connection.setDoOutput(true);
                            connection.setRequestMethod("POST");

                            outputStream = connection.getOutputStream();
                            outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
                            bufferedWriter = new BufferedWriter(outputStreamWriter);
                            bufferedWriter.write("keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=這裡是有道翻譯API");
                            bufferedWriter.flush();

                            inputStream = connection.getInputStream();
                            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                            bufferedReader = new BufferedReader(inputStreamReader);

                            String line;
                            while (true) {
                                if ((line = bufferedReader.readLine()) != null) {
                                    System.out.println(line);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                bufferedReader.close();
                                inputStreamReader.close();
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        return null;
                    }
                }.execute("http://fanyi.youdao.com/openapi.do");
            }
        });


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved