×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    help for ooops concept

    hi,

    i am using webservice to fetch data from a server.i have one single httpconnector class that fetch data from server.after fetching data i am parsing this data. and this httpconnector class is called from number of classess.

    so right now what i m doing is call a httpconnector class >http connector gets the data from server>>and call the method of called class again.

    so i just want to know that how i can set the flow at best way.

    here is my code for httpconnector class.
    String url;
    HttpConnection connection;
    InputStream inputstream;
    StringBuffer buffer;
    Registration objreg;
    Login objlogin;
    AudioplayerUI objaudio;
    CloudList cloudList;
    int flag;
    RecoverPassword recoverPassword;
    ChangePassword changePassword;
    MainScreen mainScreen;
    public HttpConnector(String url, Registration objreg)
    {
    this.url=url;
    this.objreg =objreg;
    flag = 0;
    }
    public HttpConnector(String url, Login objlogin)
    {
    this.url=url;
    this.objlogin = objlogin;
    flag = 1;

    }
    public HttpConnector(String url, AudioplayerUI objaudio)
    {
    this.url=url;
    this.objaudio = objaudio;
    flag = 2;
    }
    public HttpConnector(String url,RecoverPassword recoverPassword)
    {
    this.url=url;
    this.recoverPassword=recoverPassword;
    flag=3;
    }
    public HttpConnector(String url,ChangePassword changePassword)
    {
    this.url=url;
    this.changePassword=changePassword;
    flag=4;
    }
    public HttpConnector(String url,MainScreen mainScreen)
    {
    this.url=url;
    this.mainScreen=mainScreen;
    flag=5;
    }
    public HttpConnector(String url,CloudList cloudList)
    {
    this.url=url;
    this.cloudList=cloudList;
    flag=6;
    }
    public void run()
    {
    try
    {
    connection=(HttpConnection)Connector.open(url,Connector.READ);
    connection.setRequestMethod(HttpConnection.GET);
    if(connection.getResponseCode()==HttpConnection.HTTP_OK)
    {
    inputstream=connection.openInputStream();
    int ch;
    buffer=new StringBuffer();
    while((ch=inputstream.read())!=-1)
    {
    buffer.append((char)ch);
    }
    callback(buffer.toString());
    System.out.println(buffer.toString());
    }
    else
    {
    System.out.println("Http Response Code"+connection.getResponseCode());
    }
    }
    catch (Exception e) {
    System.out.println("Json Exception:"+e);
    }
    }

    public void callback(String data)
    {
    if(flag == 0)
    {
    objreg.parseData(data);
    }
    else if(flag == 1)
    {
    objlogin.parseData(data);
    }
    else if (flag == 2)
    {
    objaudio.parseData(data);
    }
    else if(flag==3)
    {
    recoverPassword.parseData(data);
    }
    else if(flag==4)
    {
    changePassword.parseData(data);
    }
    else if(flag==5)
    {
    mainScreen.parseData(data);
    }
    else if(flag==6)
    {
    }


    }

    thankx

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: help for ooops concept

    httpconnector class should just get the server response and send it back to the calling class which should in turn take care of data parseing. No need to call the parsing from Network layer. It sould be done by your process controller.
    Thanks,
    Ekta

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: help for ooops concept

    You have, what, five or six classes all implementing a parse() method? Make use of polymorphism here. Either they should all inherit from a common base class, or they should all implement a common interface. For example:

    Code:
    public interface Parsable {
        public void parseData(String data);
    }
    Code:
    public class Login implements Parsable {
        public void parseData(String data) {
            // specific implementation here...
        }
    }
    Then, you don't need all the different constructors, and you don't need the "flag" variable.

    Code:
    private Parsable parsable;
    
    public HttpConnector(String url, Parsable p) {
        this.url = url;
        this.parsable = p;
    }
    Oh, and notice I've declared "parsable" as private... member variables are not private by default, so make sure you specify private unless you really want the default scope.

    Your callback() method can then be removed completely, and the call to it replaced with:

    Code:
    parsable.parseData(buffer.toString());
    This hugely cuts down the amount of code, reduces the memory footprint, and means that you can add new Parsable data types easily without modifying the connector code.

    Another thing: you must close InputStreams, OutputStreams and Connector objects after you use them. If you don't, they will continue to consume resources and eventually your application will stop working.

    Don't declare all your variables as members of the class. If they are used only in one method, declare them locally. For example, connection, inputstream and buffer.

    Code:
    try {
        HttpConnection connection = Connector.open(url, Connector.READ);
        try {
            connection.setRequestMethod(HttpConnection.GET);
            if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                StringBuffer buffer=new StringBuffer();
                InputStream inputstream = connection.openInputStream();
                try {
                    int ch;
                    while ((ch = inputstream.read()) != -1) {
                        buffer.append((char) ch);
                    }
                } finally {
                    inputstream.close();
                }
                parsable.parseData(buffer.toString());
                System.out.println(buffer.toString());
            } else {
                System.out.println("Http Response Code"+connection.getResponseCode());
            }
        } finally {
            connection.close();
        }
    } catch (Exception e) {
        System.out.println("Json Exception:"+e);
    }
    Graham.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Re: help for ooops concept

    hi graham,

    thankx a lot for your great job.but i have used observer pattern for the same and now i am going to implement your way.

    please give me a suggestion related to the same.


    thanxk again.

Similar Threads

  1. What is wrong with this concept?
    By rahul.kulshreshtha in forum Qt
    Replies: 6
    Last Post: 2010-11-02, 09:54
  2. How to pitch a concept?
    By ryn004 in forum General Development Questions
    Replies: 0
    Last Post: 2009-11-30, 22:11
  3. Please help me in Thread concept
    By kannabiran.krish in forum Symbian
    Replies: 3
    Last Post: 2007-08-23, 14:04
  4. concept of PIM
    By rak_eshz in forum Mobile Java General
    Replies: 23
    Last Post: 2007-01-22, 13:12
  5. Concept for T Class
    By green_lu in forum Symbian
    Replies: 1
    Last Post: 2002-05-17, 03:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×