http client和server 通信示例

客户端发送请求,服务端接收并回应数据。网络方面刚接触,请大家多多指教。。。

cient端代码:

public class HttpAccess {
	public final static String tag = "HttpAccess";
	
	
	
	private static String packMessage(String url,String key1,String key2,String key3,String key4){
		
		HashMap<String, String> data = new HashMap<String, String>();
		Log.i(tag, "key1 "+key1+" key2 "+key2+" key3 "+key3+" key4 "+key4);
		data.put("KEY1", key1);
		data.put("KEY2", key2);
		data.put("KEY3", key3);
		data.put("KEY4", key4);
	
		String result = doPost(url+"/set/DataBaseAccess",data);
		return result;
	}
	
	
	private static String doPost(String url, Map<String,String> params) {
		// TODO Auto-generated method stub
		Log.i(tag, "url: "+url);
		String strResult = null;
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
		for(Map.Entry<String, String> entry:params.entrySet()){
			postData.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData,HTTP.UTF_8);
			post.setEntity(entity);
			HttpResponse response = httpClient.execute(post);
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				strResult = EntityUtils.toString(response.getEntity());
			}else{
				Log.e(tag,"=============Post Fail!!!!!!==============");
			}	
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strResult;
	}
}

Server端代码:

public class DataBaseAccess extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final static String tag = "DataBaseAccess";
	
	public final static String KEY1 = "KEY1";
	public final static String KEY2 = "KEY2";
	public final static String KEY3 = "KEY3";
	public final static String KEY4 = "KEY4";
	
	public final static String KEY1_DATABASEACCESS = "DATABASEACCESS";
	public final static String KEY1_GETTHUMBAIL = "GETTHUMBAIL";
	
	public final static String KEY2_MEDIASCANNER = "MEDIASCANNER";
	public final static String KEY2_MEDIACOUNT = "MEDIACOUNT";
	public final static String KEY2_MOVIEPATH = "MOVIEPATH";
	public final static String KEY2_MUSICPATH = "MUSICPATH";
	public final static String KEY2_PICTUREPATH = "PICTUREPATH";
	
	public final static int MAX_COUNT_MOVIE = 15;
	public final static int MAX_COUNT_MUSIC = 15;
	public final static int MAX_COUNT_PICTURE = 15;
	
	
	private Context mContext;
	/**
	 * Constructor of the object.
	 */
	public DataBaseAccess() {
		super();
	}
	
	private void getMusicPath(PrintWriter out, int frome,String user) {
		// TODO Auto-generated method stub
		ContentResolver mContentResolver;
		mContentResolver = mContext.getContentResolver();
		Cursor cursor = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORTRDER);
		sendPath(out,frome,MAX_COUNT_MUSIC,user,cursor,MediaStore.Audio.Media.DATA);
	}


	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		// Put your code here
		mContext = (android.content.Context) config.getServletContext().getAttribute("org.mortbay.ijetty.context");
	}
	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}
	
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Log.v(tag,"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+request.getQueryString());
		String key1 = request.getParameter(KEY1);
		String key2 = request.getParameter(KEY2);
		String key3 = request.getParameter(KEY3);
		String key4 = request.getParameter(KEY4);
		Log.v(tag,"doPost key1 "+key1+" key2 "+key2+" key3 "+key3+" key4 "+key4);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		if(key1.equals(KEY1_DATABASEACCESS)){
			if(key2.equals(KEY2_MEDIASCANNER)){
				Log.v(tag, "=======KEY2_MEDIASCANNER======");
				scanMedia(out,key3,key4);
			}else if(key2.equals(KEY2_MUSICPATH)){
				Log.v(tag, "=======KEY2_MUSICPATH======");
				getMusicPath(out,Integer.valueOf(key3).intValue(),key4);
			}
		}
		out.flush();
		out.close();
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值