android客户端与服务端交互的工具类
发布时间:2020-12-14 23:29:26 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class HttpUtil {//创建HttpClient对象public static HttpClient httpClient = new DefaultHttpClient();public static final String BASE_URL
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 public class HttpUtil { //创建HttpClient对象 public static HttpClient httpClient = new DefaultHttpClient(); public static final String BASE_URL="http://xxx.xxxx.xx.xx:8080/ticket/"; // public static final String BASE_URL="http://xxx.xxxx.xx.xx:8080/apk/"; // public static final String BASE_URL = "www.baidu.com"; /** * * @param url 发送请求的Url * @return 服务器响应的字符串 * @throws Exception * @throws InterruptedException */ public static String getRequest(final String url) throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { //创建HttpGet对象 HttpGet get = new HttpGet(url); //发送GET请求 HttpResponse response = httpClient.execute(get); //若是服务器响应成功 if(response.getStatusLine(). getStatusCode() == 200) { //获取服务器响应的字符串 String result = EntityUtils. toString(response.getEntity()); return result; } return null; } } ); new Thread(task).start(); return task.get(); } /** * * @param url 发送请求的url * @param rawParams 请求参数 * @return 响应的字符串 * @throws Exception */ public static String postRequest(final String url,final Map<String,String> rawParams) throws Exception { // ExecutorService exec=Executors.newCachedThreadPool(); // FutureTask<String> task = new FutureTask<String>( // new Callable<String>() { // @Override // public String call() throws Exception { //创建HttpPost对象 HttpPost post = new HttpPost(new URI(url)); //对较多的传递参数进行封装、 List<NameValuePair> params = new ArrayList<NameValuePair>(); for(String key : rawParams.keySet()) { //封装请求参数 params.add(new BasicNameValuePair(key,rawParams.get(key))); } //设置请求参数 post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //发送post请求 HttpResponse response = httpClient.execute(post); //若是服务器响应成功 if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //获取服务器响应的字符串 String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8); return result; }else{ return "-1"; } } // } // ); // new Thread(task).start(); // return task.get(); // } } public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { String uri = request.getRequestURI(); uri = uri.substring(uri.lastIndexOf("/")); System.out.println("uri: " + uri); if("/hotStation.do".equals(uri)) { doHotStationList(request,response); } if("/stationList.do".equals(uri)) { doStationList(request,response); } } public void doHotStationList(HttpServletRequest request,IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); List<String> hotStationList = StationService.getHostStationList(); JSONArray jsonArray = JSONArray.fromObject(hotStationList); System.out.println("传给客户端:" + jsonArray.toString()); response.getWriter().println(jsonArray.toString()); } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |