HttpClient postdemo

 new Thread(){
            
            public void run() {
                
                 HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost("http://pkuie.euming.com/urming_pkuie/system/getMyIndex?offset=0&pageSize=10&accessToken=&_=1487080350741");
                   List<NameValuePair> list = new ArrayList<NameValuePair>();
                   list.add(new BasicNameValuePair("offset", "0"));
                   list.add(new BasicNameValuePair("pageSize", "20"));
                   list.add(new BasicNameValuePair("accessToken", "0"));
                   
                   // post得到inputstream是通过实体entity设置params的时候需要entitys实体需要的是List里面是一个实现NameValuePair的实体control+T可以看到就一个类实现了
                    try {
                         post.setEntity(new UrlEncodedFormEntity(list));
                        HttpResponse execute = client.execute(post);
                        
                        int statusCode = execute.getStatusLine().getStatusCode();
                        if (statusCode ==200) {
                            
                            InputStream content = execute.getEntity().getContent();
                            ByteArrayOutputStream os = new ByteArrayOutputStream();
                            int len = 0;
                            byte[] buffer = new byte[1024];
                            
                            while((len=content.read(buffer))>0) {
                                os.write(buffer, 0, len);
                            }
                            String data = os.toString();
                            Log.e("demoPost", data);
                        }
                        
                        
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        
                        Log.e("demoError", "错误了");
                    }
                
            };
            
        }.start();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容