httpurlconnection访问网络上的数据

来源:互联网 发布:重庆seo网站诊断 编辑:程序博客网 时间:2024/05/01 02:27
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;        }    }
0 0