HttpClient的简单使用

来源:互联网 发布:c语言模块化程序设计 编辑:程序博客网 时间:2024/05/17 02:24
由于这个API已经被关闭  在Studio使用时  需要在Gradle里面找到本工程  添加一行代码,才能使用,如下:android {    useLibrary 'org.apache.http.legacy'}get请求/** * 用用HttpClient查询天气信息  聚合数据---->全国天气预报接口 * */public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    //点击事件  获取天气信息    public void httpClientGetRequest(View view){        new Thread(){            @Override            public void run() {                super.run();                getWeather();            }        }.start();    }    //用HttpClient获取天气信息    public void getWeather(){        try {            //实例化对象  相当于打开浏览器            //HttpClient其实是一个接口  DefaultHttpClient是其实现类            HttpClient client = new DefaultHttpClient();            //请求地址 :可以分为两部分            //1:接口信息            //2:参数信息            HttpGet httpGet = new HttpGet("http://v.juhe.cn/weather/index?format=2&cityname="+ URLEncoder.encode("北京","utf-8")+"&key=01b3d0d1bb6251f441ffe18a07ad655c");            //建立连接            HttpResponse response = client.execute(httpGet);            int code = response.getStatusLine().getStatusCode();            if (code == 200){                InputStream is = response.getEntity().getContent();                String json = StreamTOString.readFromNetWork(is);                Log.e( "-----------", json);            }                    } catch (IOException e) {            e.printStackTrace();        }    }}post请求/** * 用用HttpClient查询菜谱信息  聚合数据---->菜谱大全接口 * */public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void btnHttpClientPost(View v){        new Thread(){            @Override            public void run() {                super.run();               // httpClientPostReqeust("红烧肉","a7a42220c5c1c5268be7ba25af764f6c");                httpClientPostReqeust("ed1088c0c4ac34c42a270fabf0de9b5a");            }        }.start();    }    /**     *     * httpClietnt API 的pOST方式拉去数据     *     */    private String httpClientPostReqeust(String key) {        try {            //默认 取消对HttpClient的一个支持            //1.打开浏览器            HttpClient httpClient = new DefaultHttpClient();            //2.填下地址            HttpPost httpPost = new HttpPost("http://v.juhe.cn/WNXG/city");            //httpPost.addHeader("token","123544");            //设置请求参数            List<BasicNameValuePair> paramters = new ArrayList<>();            //只是添加一个了参数            //paramters.add(new BasicNameValuePair("menu", menu));            paramters.add(new BasicNameValuePair("key", key));            httpPost.setEntity(new UrlEncodedFormEntity(paramters));            //key=43434&name=zhangsan            //3.回车            HttpResponse httpResponse = httpClient.execute(httpPost);            //等待服务器响应            //200  成功  404 资源没找到  500 服务内部错误            int code = httpResponse.getStatusLine().getStatusCode();            if (code == HttpStatus.SC_OK) {                //获取服务的响应内容                InputStream is = httpResponse.getEntity().getContent();                String json = StreamTools.readFromNetWork(is);                System.out.println("http post 请求结果 : " + json);                return json;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}

0 0