post

package com.example.unit2_getdata_post_demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.google.gson.Gson;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.Menu;
import android.view.View;

/**
 * HttpURLConnection 执行post请求的案例
 * @author e531
 *
 */
public class MainActivity extends Activity {
    
    
    private Handler myHandler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            
            List<News> lists=(List<News>)msg.obj;
            
            //设置适配器,显示
            
            
        };
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void requestNews(View v){
        new Thread(){
            public void run() {
                //注意:进行post请求时,请求的地址是不包含 参数信息的(?...)
                String path="http://v.juhe.cn/toutiao/index";
                
                try {
                    //1.创建一个URL
                    URL url=new URL(path);
                    
                    //2.打开连接
                    HttpURLConnection openConnection =(HttpURLConnection) url.openConnection();
                    
                    //3.设置一些设置
                    openConnection.setRequestMethod("POST");//post必须要大写
                    openConnection.setReadTimeout(3000);
                    openConnection.setConnectTimeout(3000);
                    
                    
                    //4.设置输出参数
                    openConnection.setDoOutput(true);
                    
                    //请求的参数
                    String params="type=keji&key=c4479ad58f41e7f78a8fa073d0b1f1b5";
                    openConnection.getOutputStream().write(params.getBytes());
                    
                    
                    //5.得到响应码
                    int code=openConnection.getResponseCode();
                    if(code==200){
                        //6.得到结果
                        InputStream inputStream = openConnection.getInputStream();
                        
                        //结果
                        String content=streamToString(inputStream);
                        Log.d("zzz", content);
                        
                        //进行解析
                        Gson gs=new Gson();
                        Result result=gs.fromJson(content, Result.class);
                        Log.d("zzz", result.toString());
                        
                        //新闻列表数据
                        List<News> datas = result.getResult().getData();
                        
                        //发送消息
                        Message msg=Message.obtain();
                        msg.obj=datas;
                        myHandler.sendMessage(msg);
            
                        
                    }
                    
    
                    
                    
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
        
                
            };
        }.start();
    }
    
    /**
     * 将字节流信息的内容读取出来
     * @param is
     * @return
     */
    public String streamToString(InputStream is){
        StringBuilder builder=new StringBuilder();
        
        BufferedReader reader=new BufferedReader(new InputStreamReader(is));
        String con;
        try {
            while((con=reader.readLine())!=null){
                builder.append(con);
            }
            reader.close();
            
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
        return builder.toString();
        
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值