android 调用 返回json格式的 web service java

19 篇文章 0 订阅
1 篇文章 0 订阅

服务器端 

 生成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/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值