服务器端
生成web service 格式的json;
新建动态web 工程
添加jar包 包括 asm-3.3.1.jar jersey-bundle-1.14.jar json.jar
下载路径见 http://download.csdn.net/detail/zengxx1989/7355079
web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/frank/*</url-pattern>
</servlet-mapping>
FtoCService.java
package com.jsonws.ws;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.json.JSONObject;
@Path("/ftocservice")
public class FtoCService {
@GET
@Produces("application/json")
public Response convertFtoC() throws JSONException {
JSONObject jsonObject = new JSONObject();
double f = 98.24;
double c;
c = (f - 32) * 5 / 9;
jsonObject.put("f", f);
jsonObject.put("c", c);
String result = "@Produces(\"application/json\") Output: \n\nF to C Converter Output: \n\n" + jsonObject;
return Response.status(200).entity(""+jsonObject).build();
}
@Path("{f}/{d}")
@GET
@Produces("application/json")
public Response convertFtoCfromInput(@PathParam("f") float f,@PathParam("d") double d) throws JSONException {
JSONObject jsonObject = new JSONObject();
float celsius;
celsius = (f - 32)*5/9;
jsonObject.put("F Value", f);
jsonObject.put("C Value", celsius);
jsonObject.put("D Value", d);
String result = "@Produces(\"application/json\") Output: \n\nF to C Converter Output: \n\n" + jsonObject;
return Response.status(200).entity(result).build();
}
@Path("ftoCfromInput")
@POST
@Produces("application/json")
public Response ftoCfromInput(@FormParam("f") float f,@FormParam("d") String d) throws JSONException {
JSONObject jsonObject = new JSONObject();
float celsius;
celsius = (f - 32)*5/9;
jsonObject.put("F Value", f);
jsonObject.put("C Value", celsius);
jsonObject.put("D Value", d);
return Response.status(200).entity(""+jsonObject).build();
}
@Path("getUserInfo")
@POST
@Produces("application/json")
public Response getUserInfo(@FormParam("users") String users) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("errorcode", 200);
jsonObject.put("users", users);
System.out.println(users);
return Response.status(200).entity(""+jsonObject).build();
}
}
web service 返回xml类型的
package com.jsonws.ws;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("/ctofservice")
public class CtoFService {
@GET
@Produces("application/xml")
public String convertCtoF() {
double f, c = 36.8;
f = ((c * 9) / 5) + 32;
return "<ctofservice>" + "<celsius>" + c + "</celsius>"
+ "<ctofoutput>" + f + "</ctofoutput>" + "</ctofservice>";
}
@Path("{c}")
@GET
@Produces("application/xml")
public String converCtoFfromInput(@PathParam("c") double c){
double f ;
f = ((c * 9) / 5) + 32;
return "<ctofservice>" + "<celsius>" + c + "</celsius>"
+ "<ctofoutput>" + f + "</ctofoutput>" + "</ctofservice>";
}
}
测试方法
http://localhost/jsonws/frank/ftocservice/
两个get提交 http://localhost/jsonws/frank/ftocservice/4/5
Android 客户端 访问json web service
@Background
void queryGson(){
try {
//POST 文件提交的web service
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://{ip}/jsonws/frank/ftocservice/ftoCfromInput");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("f", "4"));
nameValuePairs.add(new BasicNameValuePair("d", "中国"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
HttpResponse response = httpClient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
//String str = response.getEntity().toString();
String str = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println("json ========"+str);
Message msg = Message.obtain();
msg.obj = str;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
参考文档 http://crunchify.com/how-to-build-restful-service-with-java-using-jax-rs-and-jersey/