开发者

Program in Android to upload .json file & recieve .json response

I am supposed to develop a program in android which uploads a .json file to a webservice & get back the response in .json format & 开发者_Go百科parse it. Can anybody tell me how to upload a json file & any webservice which takes a .json file,validates it & returns a .json file? thanks.


An ideal setup would be

Android App <-> GAE

If you need the web-service to just check you json file...simply read the json content at the server into a JSON object and if no exception is thrown you are good to go with the response to the client.

I have provided code that uses Restlet (restlet.org) on both sides.


Android App


Client client = new Client(Protocol.HTTP);
Request req = new Request();
req.setMethod(Method.POST); // can be Method.GET
req.setResourceRef(new Reference(/* SERVER URL */+ "/jsonservice"));
req.getCookies().add(GAuth.getCookie());
/* Build your JSONObject */
req.setEntity(/* JSONObject */.toString(), MediaType.APPLICATION_JSON);
req.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
Response resp = client.handle(req);
if (resp.getStatus() == Status.SUCCESS_OK)
{
    // resp.getEntity().getText() -> The JSON string returned by GAE
    JSONObject jo=new JSONObject(resp.getEntity().getText());
    /* Use your JSON object */
}

GAE App


GAE : war/WEB-INF/web.xml

<!-- Servlets -->
<servlet>
  <servlet-name>MyApplication</servlet-name>
  <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
  <init-param>
      <param-name>org.restlet.application</param-name>
      <param-value>com.mypackage.MyApplication</param-value>
  </init-param>
</servlet>

<!-- Servlet Mappings -->
<servlet-mapping>
  <servlet-name>MyApplication</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

Restlet : MyApplication.java

package com.mypackage;

public class MyApplication extends Application
{
  @Override
  public Restlet createInboundRoot()
  {
      Router router = new Router(getContext());
      router.attach("/jsonservice", MyJsonService.class);
      return router;
  }
}

Restlet : MyRouter.java

package com.mypackage;

public class MyJsonService extends ServerResource
{
  @Post("json:json") // can be @Get("json")
  public Representation jsonProcessor(Representation entity)
  {
      Representation resp = null;
      JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
      /* 
          Do Someting with the JSON object
          .....
          .....
          .....
          Build your JSON response object. 
          You will use this object below.
      */
      setStatus(Status.SUCCESS_OK);
      resp = new JsonRepresentation(/* JSONObject */.toString());
      return resp;
  }
}


Uploading json file is not different from uploading any other file type. There is an example here. It explains how to upload a file to php server.

And http://json.org/ has a java library you can use to create/parse the json objects. As the comment below points, there is a json library included in android.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜