Right now whenever my project do have a web-service at back end. I am used to create my project with this structure/pattern.
Project
- HttpMethods Package
- HttpGetThread
- HttpPostThread
- HttpMultipartPostThread
- HttpGetThread
- Interfaces Package
- IPostResponse
The code I have been writing in my these JAVA files are,
IPostResponse.java
public interface IPostResponse {
public void getResponse(String response);
}
HttpGetThread.java
public class HttpGetThread extends Thread {
private String url;
private final int HTTP_OK = 200;
private IPostResponse ipostObj;
public HttpGetThread(String url, IPostResponse ipostObj) {
this.url = url;
this.ipostObj = ipostObj;
}
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == HTTP_OK) {
InputStream inputStream = httpResponse.getEntity().getContent();
int bufferCount = 0;
StringBuffer buffer = new StringBuffer();
while ((bufferCount = inputStream.read()) != -1) {
buffer.append((char) bufferCount);
}
ipostObj.getResponse(buffer.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Same way in HttpPost
and HttpMultipartPost
classes by extending Thread
and having one constructor and a run method.
Then,
I implement the interface to one activity and extend that main activity to all other activities and to get response and make call by creating object of Http classes with parameters and call obj.start();
I still believe that : I am lacking many things or this structure is very poor.
I need to know that, for an Android application to implement Web Service calls in mostly all the activities and have code re-usability which Pattern/Structure should I follow?
I just have seen how Facebook make web service call, for example to Login/Logout it's having Login and Logout listeners.
Is there any blog/article/answers which is well-documented for the same? Please, can any user share his/her excellent experience and solution for the same?
I am more interested in "How should my Class and Interface look like, which kind of methods it should have?"