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 list = new ArrayList();
                   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();

你可能感兴趣的:(HttpClient postdemo)