中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android客戶端與服務(wù)端交互的工具類

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

客戶端:

public class HttpUtil {
	
	//創(chuàng)建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 發(fā)送請求的Url
	 * @return 服務(wù)器響應(yīng)的字符串
	 * @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 {
					//創(chuàng)建HttpGet對象
					HttpGet get = new HttpGet(url);
					//發(fā)送GET請求
					HttpResponse response = httpClient.execute(get);
					//若是服務(wù)器響應(yīng)成功
					if(response.getStatusLine().
							getStatusCode() == 200) {
						//獲取服務(wù)器響應(yīng)的字符串
						String result = EntityUtils.
								toString(response.getEntity());
						return result;
					}
					return null;
				}
			}
		);
		new Thread(task).start();
		return task.get();
	}
	
	/**
	 * 
	 * @param url 發(fā)送請求的url
	 * @param rawParams 請求參數(shù)
	 * @return 響應(yīng)的字符串
	 * @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 {
					//創(chuàng)建HttpPost對象
					HttpPost post = new HttpPost(new URI(url));
					//對較多的傳遞參數(shù)進行封裝、
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					for(String key : rawParams.keySet()) {
						//封裝請求參數(shù)
						params.add(new BasicNameValuePair(key, rawParams.get(key)));
					}
					//設(shè)置請求參數(shù)
					post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
					//發(fā)送post請求
					HttpResponse response = httpClient.execute(post);
					//若是服務(wù)器響應(yīng)成功
					if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
						//獲取服務(wù)器響應(yīng)的字符串  
						String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
						return result;
					}else{
						return "-1";
					}
					
				}
//			}
//		);
//		new Thread(task).start();
//		return task.get();
//	}
	
	
}

注意:連接本地服務(wù)器時,最好使用ipv4地址而不是localhost。


服務(wù)端:

寫一個servlet接收,判斷客戶端發(fā)送的是什么請求uri

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);
		}
	}

如doHotStationList是這樣寫的:

public void doHotStationList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, 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());
	}

我這里數(shù)據(jù)交互使用的是json.

客戶端獲取服務(wù)端傳遞過來的json數(shù)據(jù)再解析成pojo對象即可。

標(biāo)簽: 本地服務(wù)器 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Zookeeper的增刪改查Java代碼

下一篇:Java根據(jù)兩點的經(jīng)緯度來計算之間的距離