PDA

View Full Version : Servlet Will response JSON object and will read json object using sencha



chilukuri
14 Feb 2011, 4:33 AM
Hi,

I am new to Sencha touch , I am using this scripts in android sdk using phone gap frame work.

I have implemented servlet which will respond JSON object.

JSONServlet .java

package com.bms;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class JSONServlet
*/
public class JSONServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public JSONServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
JSONArray arr = new JSONArray();
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
JSONObject obj4 = new JSONObject();
JSONObject obj5 = new JSONObject();
JSONObject obj6 = new JSONObject();

obj1.put("text", "abc");
obj1.put("url", "abc.com");
obj1.put("leaf", true);

obj4.put("text", "klm");
obj4.put("url", "klm.com");
obj4.put("leaf", true);

obj5.put("text", "xyz");
obj5.put("url", "xyz.com");
obj5.put("leaf", true);

obj6.put("text", "pqr");
obj6.put("url", "pqr.com");
obj6.put("leaf", true);
arr.add(obj1);
arr.add(obj4);
arr.add(obj5);
arr.add(obj6);
obj2.put("items",arr);

out.println(obj2);
//out.println(obj3.toString());


}
}


and In sencha script, I am calling the servlet and try to display the servlet output (Json format)


Ext.setup({
onReady : function() {
var cStore = new Ext.data.Store({
autoLoad: true,
proxy: {
type: 'ajax',
url: 'http://localhost:7001/JSONServlet/JSONServlet',
reader: {
type: 'json',
root: 'items',
totalProperty: 'results'
}
}
,fields: ['text', 'url']
});




}

});


While executing the script i got below exception

02-14 17:39:05.553: ERROR/Web Console(320): TypeError: Result of expression 'records' [undefined] is not an object. at file:///android_asset/www/sencha-touch-debug.js:7209 (file:///android_asset/www/sencha-touch-debug.js:7209)

I found one more thing , request is not hitting to the server

help me on this , how to get the data?

sobrle
14 Apr 2011, 11:59 PM
First,

your servlet should be just url: 'JSONServlet',

I think your response type should be response.setContentType("application/json");

I hope it helps:)