the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


android和javaEE通信的代码片

作者:the5fire | 标签:     | 发布:2011-07-15 2:39 p.m. | 阅读量: 7381, 7140
随着android的流行度越来越高,越来越多的人投身到android的开发中,其中当然不乏一些javaEE方面的程序员。对于转入到android开发行列的javaEE程序员来说,除了对java的使用相当熟悉之外,还有一个优势就是对Web服务器的熟悉。
对于开发手机办公系统,尤其是要和原先的系统进行对接,这就需要android开发人员除了懂android开发,也要懂的JavaEE的开发(重点在服务器端),可谓是要能上得了“厅堂”,下得了“厨房”。
这几天看了下android的程序,也写了一个。今天学习了一下新浪微博的SDK,整理出来一个简单的HttpClient类(目前是最简单的,以后不断完善),供以后使用。
上代码:

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


还有一个类:

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项目进行交互了。 - from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: