android的get跟post 代码

package csdn.com.login;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class Network2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network2);

    }


    public void onClick_get(View view){
        new Thread(networkTask_get).start();
    }

    public void onClick_post(View view){
        new Thread(networkTask_post).start();
    }











    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            String val = data.getString("value");
            Log.i("mylog=============", "请求结果为-->" + val);
            // UI界面的更新等相关操作
        }
    };

    Runnable networkTask_get = new Runnable() {
        @Override
        public void run() {
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("value", run_get());
            msg.setData(data);
            handler.sendMessage(msg);
        }
    };

    Runnable networkTask_post = new Runnable() {
        @Override
        public void run() {
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("value", run_post());
            msg.setData(data);
            handler.sendMessage(msg);
        }
    };












    public String run_get() {
        HttpURLConnection connection = null;
        try {
            URL url = new URL("http://10.0.0.108/tp/public/index.php/index/index/t?month=12&day=1");
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法,默认是GET
            connection.setRequestMethod("GET");
            // 设置字符集
            connection.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型
            connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
            // 设置请求参数,可通过Servlet的getHeader()获取
            connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
            // 设置自定义参数
            connection.setRequestProperty("MyProperty", "this is me!");

            if (connection.getResponseCode() == 200) {
                InputStream is = connection.getInputStream();
                //    result = StringStreamUtil.inputStreamToString(is);

                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                String result = sb.toString();
                return result;


            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        return null;

    }

    public String run_post() {
        HttpURLConnection connection = null;
        try {
            URL url = new URL("http://10.0.0.108/tp/public/index.php/index/index/t");
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求方式
            connection.setRequestMethod("POST");
            // 设置编码格式
            connection.setRequestProperty("Charset", "UTF-8");
            // 传递自定义参数
            connection.setRequestProperty("MyProperty", "this is me!");
            // 设置容许输出
            connection.setDoOutput(true);
            OutputStream os = connection.getOutputStream();
     

            //将该字节数组作为请求体
            byte[]   requestBody = new String("name=旭&age=32").getBytes("UTF-8");
            //将请求体写入到conn的输出流中
            os.write(requestBody);


            os.flush();
            os.close();

            // 获取返回数据
            if(connection.getResponseCode() == 200){
                InputStream is = connection.getInputStream();
            //    result = StringStreamUtil.inputStreamToString(is);


                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                String result = sb.toString();
                return result;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(connection!=null){
                connection.disconnect();
            }
        }

        return null;
    };



}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值