前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android和javaEE通信的代码片

android和javaEE通信的代码片

作者头像
the5fire
发布2019-02-28 15:30:47
4190
发布2019-02-28 15:30:47
举报
文章被收录于专栏:Python程序员杂谈

随着android的流行度越来越高,越来越多的人投身到android的开发中,其中当然不乏一些javaEE方面的程序员。对于转入到android开发行列的javaEE程序员来说,除了对java的使用相当熟悉之外,还有一个优势就是对Web服务器的熟悉。

对于开发手机办公系统,尤其是要和原先的系统进行对接,这就需要android开发人员除了懂android开发,也要懂的JavaEE的开发(重点在服务器端),可谓是要能上得了“厅堂”,下得了“厨房”。

这几天看了下android的程序,也写了一个。今天学习了一下新浪微博的SDK,整理出来一个简单的HttpClient类(目前是最简单的,以后不断完善),供以后使用。

上代码:

代码语言:javascript
复制
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class HttpClient {
	public static int httpRequest(String url, PostParameter[] postParams,
             String httpMethod) {
		int responseCode = -1;
		try {
			HttpURLConnection con = null;
			OutputStream osw = null;
			try {
				con = (HttpURLConnection) new URL(url).openConnection();

				con.setDoInput(true);
				if (null != postParams || "POST".equals(httpMethod)) {
				   con.setRequestMethod("POST");
				   con.setRequestProperty("Content-Type",
				           "application/x-www-form-urlencoded");
				   con.setDoOutput(true);
				   String postParam = "";
				   if (postParams != null) {
				   		postParam = encodeParameters(postParams);
				   }
				   byte[] bytes = postParam.getBytes("UTF-8");

				   con.setRequestProperty("Content-Length",
				           Integer.toString(bytes.length));
				   osw = con.getOutputStream();
				   osw.write(bytes);
				   osw.flush();
				   osw.close();
				} 
				responseCode = con.getResponseCode();
				System.out.println("responseCode:"+responseCode);
				System.out.println("responseMsg:"+con.getResponseMessage());
			} finally {

			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return responseCode;
	}

	private static String encodeParameters(PostParameter[] postParams) {
	    StringBuffer buf = new StringBuffer();
	    for (int j = 0; j < postParams.length; j++) {
	        if (j != 0) {
	            buf.append("&");
	        }
	        try {
	            buf.append(URLEncoder.encode(postParams[j].getName(), "UTF-8"))
	            	.append("=").append(URLEncoder.encode(postParams[j].getValue(), "UTF-8"));
	        } catch (java.io.UnsupportedEncodingException neverHappen) {
	        }
	    }
	    return buf.toString();
	}

	public static void main(String[] args) {
		PostParameter[] postParameters = new PostParameter[2];
		postParameters[0] = new PostParameter("loginName","demo");
		postParameters[1] = new PostParameter("password","demo");
		httpRequest("http://localhost:8090/test/user/loginAction.action", postParameters,
	             "POST");
	}
}

还有一个类:

代码语言:javascript
复制
public class PostParameter implements java.io.Serializable, Comparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	public PostParameter(String name, String value) {
		super();
		this.name = name;
		this.value = value;
	}
public PostParameter(){

}
	private String name;
	private String value;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}


}

目前只是发送请求,还没有完成接受返回请求的方法。

通过这个代码就可以使android程序和JAVAEE项目进行交互了。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-07-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档