Android HttpClient POST JSON Restful-web-services

@Override
        protected String doInBackground(String... arg0) {
            Gson gson = new Gson();
            String json = gson.toJson(map);
            
            HttpPost httpPost = new HttpPost(WR.URL_YJFK);
            
            String result = null;
            
            try {
                StringEntity entity = new StringEntity(json, HTTP.UTF_8);
                
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
                HttpClient client = new DefaultHttpClient();
                HttpResponse response = client.execute(httpPost);
                
                if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    InputStream in = response.getEntity().getContent();
                    result = StrUtil.readString(in);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return result;
        }

 

posted @ 2015-08-14 17:30  yshy  阅读(1155)  评论(0编辑  收藏  举报