最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android使用url connection示例(get和post數(shù)據(jù)獲取返回數(shù)據(jù))

 更新時間:2014年01月12日 09:47:37   作者:  
這篇文章主要介紹了android使用URLConnection來get和post數(shù)據(jù)獲取返回的數(shù)據(jù),大家參考使用吧

一定要加上對Sd卡讀寫文件的權(quán)限聲明,以及訪問網(wǎng)絡(luò)的權(quán)限

復(fù)制代碼 代碼如下:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

get /post 工具

復(fù)制代碼 代碼如下:

package com.act262.whpj.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import android.os.Environment;
import android.util.PrintStreamPrinter;

/**
 * 用于get或者post數(shù)據(jù)
 */
public class GetPostUtil {
    public static final String TAG = "GetPostUtil Debug";

    /**
     * @param url
     *            傳入的url,包括了查詢參數(shù)
     * @return 返回get后的數(shù)據(jù)
     */
    public static String sendGet(String url) {
        String result = "";
        // String
        URL realURL = null;
        URLConnection conn = null;
        BufferedReader bufReader = null;
        String line = "";
        try {
            realURL = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("url 格式錯誤");
        }

        try {
            conn = realURL.openConnection();
            // 設(shè)置連接參數(shù)...conn.setRequestProperty("xx", "xx");

            conn.setConnectTimeout(10000); // 10s timeout
            // conn.setRequestProperty("accept", "*/*");
            // conn.setRequestProperty("", "");

            conn.connect(); // 連接就把參數(shù)送出去了 get方法

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("連接錯誤");
        }

        try {
            bufReader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "gb2312"));

            while ((line = bufReader.readLine()) != null) {
                result += line + "\n";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("讀取數(shù)據(jù)錯誤");
        } finally {
            // 釋放資源
            if (bufReader != null) {
                try {
                    bufReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return result;
    }

    /**
     * @param url
     * @param param
     *            查詢參數(shù) ,形式如 name=xx&age=xx&sex=xx
     * @return
     */
    public static String sendGet(String url, String param) {
        return sendGet(url + "?" + param);
    }

    /**
     * @param url
     *            指定的url,不包括查詢參數(shù)
     * @param param
     *            查詢參數(shù) 形式如 name=xx&age=xx&sex=xx
     * @return 返回post后的數(shù)據(jù)
     */
    public static String sendPost(String url, String param) {
        String result = "";
        URL realURL = null;
        BufferedReader bufReader = null;
        // PrintWriter printWriter = null;
        PrintStreamPrinter out = null;
        URLConnection connection = null;
        String line = "";
        try {
            realURL = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            connection = realURL.openConnection();
            // 設(shè)置為可輸入輸出 post的模式,而且在輸出之前不能獲取輸入的數(shù)據(jù),否則報錯
            connection.setDoOutput(true);
            connection.setDoOutput(true);

            // 已經(jīng)連接了,所以不能再用connect(),否則報錯的

            out = new PrintStreamPrinter(new PrintStream(
                    connection.getOutputStream()));
            out.println(param);
            //
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bufReader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "gb2312"));
            while ((line = bufReader.readLine()) != null) {
                result += line + "\n";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 釋放資源
            try {
                if (bufReader != null) {
                    bufReader.close();
                }
                if (out != null) {
                    //
                }
            } catch (IOException e2) {
                // TODO: handle exception
            }

        }
        return result;
    }

    public static void saveFile(String content) {

        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath(), "file.html");
        if (!file.exists()) {
            try {
                boolean status = file.createNewFile();

                System.out.println("is create new file :" + status);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        PrintWriter pw = null;
        try {
            FileWriter fw = new FileWriter(file);
            // pw = new PrintWriter(new Date() + ".html");
            // pw.println(content);

            fw.write(content);
            fw.flush();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (pw != null) {
                pw.close();
            }
        }
    }

}

測試類

復(fù)制代碼 代碼如下:

package com.act262.whpj.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.act262.whpj.R;
import com.act262.whpj.utils.GetPostUtil;

public class StartActivity extends Activity {

    Button get, post;
    TextView showTextView;
    Handler handler;
    String result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        get = (Button) findViewById(R.id.get);
        post = (Button) findViewById(R.id.post);
        showTextView = (TextView) findViewById(R.id.show);
        handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 0x123) {
                    showTextView.setText(result);
                }
            }
        };
        post.setOnClickListener(new ButtonListener());
        get.setOnClickListener(new ButtonListener());
    }

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

    class ButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.get:

                new Thread() {

                    public void run() {
                        result = GetPostUtil
                                .sendGet("http://www.baidu.com");
                        handler.sendEmptyMessage(0x123);// 通知UI線程更新界面
                        // Log.d(GetPostUtil.TAG, result);
                        System.out.println(result);
                        GetPostUtil.saveFile(result);
                    }
                }.start();
                showTextView.setText(result);
                break;
            case R.id.post:
                new Thread() {
                    public void run() {
                        result = GetPostUtil
                                .sendPost(
                                        "http://www.baidu.com",
                                        "null");
                        handler.sendEmptyMessage(0x123);// 通知UI線程更新界面
                        Log.d(GetPostUtil.TAG, result);
                    }
                }.start();
                showTextView.setText(result);
                break;
            default:
                break;
            }
        }
    }
}

相關(guān)文章

  • Android多媒體應(yīng)用使用MediaPlayer播放音頻

    Android多媒體應(yīng)用使用MediaPlayer播放音頻

    這篇文章主要為大家詳細(xì)介紹了Android多媒體應(yīng)用使用MediaPlayer播放音頻,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android閃屏效果實(shí)現(xiàn)方法

    Android閃屏效果實(shí)現(xiàn)方法

    這篇文章主要介紹了Android閃屏效果實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android閃屏效果的實(shí)現(xiàn)原理及相關(guān)功能與布局設(shè)置技巧,需要的朋友可以參考下
    2016-01-01
  • 基于Android實(shí)現(xiàn)顏色漸變動畫效果

    基于Android實(shí)現(xiàn)顏色漸變動畫效果

    本文主要給大家介紹了Android實(shí)現(xiàn)顏色漸變動畫效果,實(shí)現(xiàn)這樣的一個動畫漸變的效果很簡單,只需要兩步,第一步用GradientDrawable實(shí)現(xiàn)兩個顏色之間的漸變效果,第二步用屬性動畫實(shí)現(xiàn)顏色變化的過程,需要的朋友可以參考下
    2024-01-01
  • Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換

    Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)excel/pdf/word/odt/圖片之間的相互轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-04-04
  • Android仿京東手機(jī)端類別頁

    Android仿京東手機(jī)端類別頁

    這篇文章主要為大家詳細(xì)介紹了Android仿京東手機(jī)端類別頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 安卓(Android)開發(fā)之統(tǒng)計App啟動時間

    安卓(Android)開發(fā)之統(tǒng)計App啟動時間

    當(dāng)大家要改善APP啟動速度優(yōu)化的時候,首先要知道App的啟動時間,那么改如何統(tǒng)計時間呢,下面我們一起來看看。
    2016-08-08
  • Android對話框AlertDialog詳解

    Android對話框AlertDialog詳解

    本文詳細(xì)講解了Android對話框AlertDialog的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • Flutter實(shí)現(xiàn)切換應(yīng)用時隱藏應(yīng)用預(yù)覽

    Flutter實(shí)現(xiàn)切換應(yīng)用時隱藏應(yīng)用預(yù)覽

    如果您要顯示敏感數(shù)據(jù),例如錢包金額,或者只是當(dāng)?shù)卿洷韱物@示插入的密碼清晰時,當(dāng)您不在應(yīng)用程序中時,您必須隱藏敏感數(shù)據(jù)。本文將利用Flutter實(shí)現(xiàn)切換應(yīng)用時隱藏應(yīng)用預(yù)覽,需要的可以參考一下
    2022-06-06
  • Android 調(diào)用系統(tǒng)相冊選擇照片

    Android 調(diào)用系統(tǒng)相冊選擇照片

    這篇文章主要介紹了Android 調(diào)用系統(tǒng)相冊選擇照片的方法,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • Android仿微信界面的導(dǎo)航以及右上角菜單欄效果

    Android仿微信界面的導(dǎo)航以及右上角菜單欄效果

    這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧
    2018-05-05

最新評論

积石山| 浦县| 中超| 通山县| 虹口区| 施甸县| 保亭| 常熟市| 永州市| 察隅县| 新源县| 阳曲县| 阿拉善左旗| 高陵县| 连南| 托克托县| 沙雅县| 北川| 错那县| 泾阳县| 龙海市| 泾源县| 库车县| 龙岩市| 溧水县| 拉萨市| 浦北县| 伊金霍洛旗| 兴和县| 格尔木市| 昌都县| 镶黄旗| 黄冈市| 临清市| 安阳市| 奉化市| 博罗县| 磐石市| 政和县| 济源市| 和林格尔县|