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 data = new HashMap();
		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 params) {
		// TODO Auto-generated method stub
		Log.i(tag, "url: "+url);
		String strResult = null;
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		List postData = new ArrayList();
		for(Map.Entry 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. 
* * @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.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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.
* * 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(); } }





你可能感兴趣的:(android)