Binding Resultset Data to Getter Setter Method to Form JSON String

I am trying to form a JSON using GSON. But my final JSON formed returns null.My result set contains all the available data but the problem persist when forming JSON from resultset.

Any Help,My code below.

My DAO CLASS–ServiceAccess

package com.rest.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import org.codehaus.jackson.annotate.JsonProperty;

import com.rest.constants.Constants;


public class ServiceAccess 
{
    public ArrayList<ServiceBean> GetServices() throws Exception
    {

        ArrayList<ServiceBean> feedServices = new ArrayList<ServiceBean>();
        try
        {

            Connection con;
            Statement stmt=null;

             Class.forName(Constants.dbClass);
                 con = DriverManager.getConnection("jdbc:oracle:thin:@XX.XX.XX.XXX:152X:orcl10g","XXXX","XXXXX");
             stmt = (Statement) con.createStatement();
             String sqlQuery1="select SSCODE as SERVICECODE,SSDESC as SERVICEDESC from ksubservice order by SSCODE";
             System.out.println("printing sql query1 :"+sqlQuery1);
             ResultSet rst = stmt.executeQuery(sqlQuery1);

             while (rst.next()) 
             {


            ServiceBean ServiceObjects=new ServiceBean();
            ServiceObjects.setServiceCode(rst.getString("SERVICECODE").toString());   
            ServiceObjects.setServiceDesc(rst.getString("SERVICEDESC").toString()); 


        }
        System.out.println(feedServices);
        return feedServices;


        }
        catch(Exception e)
        {
            throw e;
        }



    }


}

My Bean Class–ServiceBean

package com.rest.bean;

public class ServiceBean 
{

private String ServiceCode;
private String ServiceDesc;

public String getServiceCode()
{
    return ServiceCode;
}

public void setServiceCode(String ServiceCode)
{
    this.ServiceCode=ServiceCode;
}

public String getServiceDesc()
{
    return ServiceDesc; 
}

public void setServiceDesc(String ServiceDesc)
{
    this.ServiceDesc=ServiceDesc;
}

}

My Model Class–ServiceArray

package com.rest.bean;



import java.util.ArrayList;

import com.rest.database.DBConnection;


public class ServiceArray 
{

    public ArrayList<ServiceBean> GetServices()throws Exception
    {
    ArrayList<ServiceBean> serviceData = null;
    try
    {
     ServiceAccess accessObjects=new ServiceAccess();
     serviceData=accessObjects.GetServices();

    } 
    catch (Exception e) 
    {
     throw e;
    }
    return serviceData;

    }


}

My JsonObject Converter Class

package com.rest.bean;

import java.util.ArrayList;
import com.google.gson.Gson;
import com.rest.bean.ServiceBean;

public class DataTransformer 
{

    public static String DataFeed(ArrayList<ServiceBean> feedData)
    {
    String feeds = null;
    Gson gson = new Gson();
    feeds = gson.toJson(feedData);
    return feeds;
    }



}

Answer

Json Http Request

String url = "http://www.myurl.com/data.php";

BufferedReader inStream = null; 
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet(url);

try {

       HttpResponse response = httpClient.execute(httpRequest);
       inStream = new BufferedReader(
       new InputStreamReader(
       response.getEntity().getContent()));

       StringBuffer buffer = new StringBuffer("");
       String line = "";

       while ((line = inStream.readLine()) != null) {
                buffer.append(line);
            }
            inStream.close();

            reply = buffer.toString();  
        } catch (Exception e) {
                //Handle Exception
        }

This should get the json reply if it is issued corrected form the url

Leave a Reply

Your email address will not be published. Required fields are marked *