Using JSON in vaadin

Hello I am new to vaadin and want to use JSONObject. The scenario is as such:
The android device capture the image and convert it into byte using Base64.java and then to String. After that it put the String into JSONObject and send it to vaadin server(My Application). I doesn’t know how to accept JSONObject in Vaadin Application.i.e i want to know how vaadin will handle JSONObject send through HttpPost.
Anyone please help me how should i do it.

Bonjour,
first, I suppose that you just have to add the JSON library in the lib folder on the server side.

Yes, i have added JSON jar file in lib folder but when i parses the string it is throwing NullPointerException.

You give few/no details about how you send the data, how you receive it on the server side (a separate servlet? customized Vaadin servlet?) etc.

In any case, try a debugger on the server side and set an exception breakpoint on NullPointerException. Then see what comes in and what is null.

The code for sending json object from android device is as such:

                            HttpClient client = new DefaultHttpClient();
			JSONObject jsonObject=new JSONObject();
			try {
				jsonObject.put("imageName", imageNm);
				jsonObject.put("imageSend", bal);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				
			serverUrl = serverUrl+"UploadImage/" ;
			HttpPost post = new HttpPost(serverUrl);
			HttpResponse httpResponse = null;
			try {
				
				StringEntity entity=new StringEntity(jsonObject.toString());
			entity.setContentType("application/json;char-set=UTF-8");
			entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;char-set=UTF-8"));
				
				post.setEntity(entity);
				httpResponse = client.execute(post);
				response = EntityUtils.toString(httpResponse.getEntity());
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

I don’t have the relevant code at hand, but from memory, on the receiving side, your options might include

  • Using a
    URIHandler
  • Mapping that path to a separate servlet (in the same WAR) in your web.xml and handling the upload there
  • Intercepting (Abstract)ApplicationServlet.service() and handling it there

Each of the approaches has its advantages and limitations. Are you using one of these to handle the post?

By default, everything under the Vaadin application context path is interpreted as being in the internal format Vaadin uses on the network.

code on the server side(vaadin):

                      public class SPTURIHandler implements URIHandler {
                      if(relativeUri.equalsIgnoreCase("UploadImage")){
		      System.out.println("In UploadImage");
		
	
	//	System.out.println("Header type"+ds.getContentType());
		   Map<String, String[]> parameters = SPTSession.getCurr_params();
		     if (parameters == null) {
			System.out.println("Image parameter not received");
			return ReturnValue2Client("Image not received on server !!!");
		}
	
		for(Iterator<String> it=parameters.keySet().iterator();it.hasNext();){
			String key=(String)it.next();
			[b]

String value = ((String) parameters.get(key))[0]
;
[/b]
if(key.equalsIgnoreCase(“image”)){
ParamImage=value;
if(ParamImage!=null){
ReturnValue2Client(“Image Received on Server”);
}else{
ReturnValue2Client(“Image value is null”);
}

			}
			
			
		}

when i am sending JSONObject is throwing NullPointerException as value (in bold) is null.

There seem to be parameters that have the value null.
Check what your SPTSession.getCurr_params() returns, put a breakpoint there to see more details.

SPTSession class :

public class SPTSession implements TransactionListener, Serializable {
private static ThreadLocal instance = new ThreadLocal();
private Map<String, String> curr_params;

   	public SPTSession (Application app) {
    this.app = app;
    this.curr_loc = null;
    this.curr_sp = null;
    this.usr = new User();
    // It's usable from now on in the current request
    instance.set(this);
}

    public static Map<String, String[]> getCurr_params() {
	if (instance.get() == null)
		return null;
	else
		return instance.get().curr_params;
}

public static void setCurr_params(Map<String, String[]> curr_params) {
	if (instance.get() != null)
		instance.get().curr_params = curr_params;
}

Please first debug this yourself rather than just posting partial code - it will be faster for everybody.

Put a breakpoint in both getCurr_params() and setCurr_params(), see when they are hit and what are the values in each. Check that the setter is correctly called before the getter (e.g. from a properly registered TransactionListener). Based on what you see, try to see if you can find out what can cause whatever is null to be null.