httpurlconnection访问网络上的数据

public class MainActivity extends Activity {
    @ViewInject(R.id.button_get)
    private Button getpButton;
    @ViewInject(R.id.button_post)
    private Button postbButton;
    private URL url;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewUtils.inject(MainActivity.this);
        try {
            url = new URL(
                    "http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @OnClick(value = { R.id.button_get, R.id.button_post })
    public void myClick(View v) {
        switch (v.getId()) {

        //1.get请求
        case R.id.button_get:
            new Thread() {
                public void run() {
                    try {
                    //通过url打开连接
                        HttpURLConnection connection = (HttpURLConnection) url
                                .openConnection();
                        connection.setRequestMethod("GET");
                        //通对HttpURLConnection 对象进行连接
                        connection.connect();
                        BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                        connection.getInputStream()));

                        StringBuffer sBuffer = new StringBuffer();
                        String str = null;
                        while ((str = br.readLine()) != null) {
                            sBuffer.append(str);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                };
            }.start();
            Toast.makeText(MainActivity.this, "get请求成功", 0).show();
            break;


//2.post请求
        case R.id.button_post:
            AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
                private ProgressDialog progressDialog;
                // 主线程(数据加载成功之前进行一些准备工作)
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("正在努力加载,请稍后......");
                    progressDialog.setTitle("提示信息");
                    progressDialog.show();
                }
                // 主线程,当耗时任务完成是会调用此方法
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    progressDialog.dismiss();
                }
                // 子线程
                protected String doInBackground(String... params) {
                    try {
                        url = new URL(
                                "http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee");
                        HttpURLConnection connection = (HttpURLConnection) url
                                .openConnection();

                connection.setRequestMethod("POST");
                        connection.connect();

                        // 如果使用post请求,需要先将请求的参数发送给服务器
                        PrintWriter pWriter = new PrintWriter(
                                connection.getOutputStream());
                        pWriter.print("type=yule&key=c8fe066ae002d20891fbc48a1783a1ee");
                        pWriter.close();

                        // 读取数据
                        BufferedReader bReader = new BufferedReader(
                                new InputStreamReader(
                                        connection.getInputStream()));
                        StringBuffer stringBuffer = new StringBuffer();
                        String str = null;
                        while ((str = bReader.readLine()) != null) {
                            stringBuffer.append(str);
                        }
                        Log.i("myTag", stringBuffer.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

            };
            task.execute();
            Toast.makeText(MainActivity.this, "post请求成功", 0).show();
            break;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值