麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

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

2020-04-11 11:56:46
字體:
供稿:網(wǎng)友

一定要加上對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;
            }
        }
    }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中国a级黄色片 | 2019中文字幕在线播放 | 国产精品91在线 | 欧美精品一区二区视频 | 二区三区四区视频 | 欧美一级视频免费看 | 国产毛片自拍 | 久久久无码精品亚洲日韩按摩 | 一级做人爱c黑人影片 | 在线成人免费观看视频 | 色婷婷av一区二区三区久久 | 国产精品99久久99久久久二 | 99国产精品自拍 | 久草视频国产在线 | 久久99国产伦子精品免费 | 久综合| 成人一区二区三区在线 | 国产宾馆3p国语对白 | 啊~用cao嗯力cao烂我视频 | 欧美在线观看禁18 | 久久成人视屏 | 亚洲骚图| 国产午夜精品理论片a级探花 | 国产一级一片免费播放 | 欧美成人一级片 | 精品国产一区二区三 | 免费永久在线观看黄网 | 丰满年轻岳中文字幕一区二区 | 久夜草| 视频一区二区三区在线播放 | 精品中文视频 | 免费观看黄色一级视频 | 99国内精品视频 | 欧美性受xxxx人人本视频 | 91精品国产乱码久久久久 | 欧美成人国产va精品日本一级 | 久久久精品视频在线观看 | 久久17| 18被视频免费观看视频 | 国产精品久久久久久久久久东京 | 亚洲一区二区三区精品在线观看 |