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.