HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Android AsyncTask, HTTP Request and Parsing

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
androidrequesthttpparsingandasynctask

Problem

I am working on Android applications for last 2 years and about 80% applications I have developed involve web-services consumption followed by some sort of XML or JSON parsing. Initially, when I had to call a web service, I had to create a separate AsyncTask class and then parsing was also the part of that AsyncTask class. The HTTP request sending and other stuff was being handled in my ConnectionManager class. Then I realized there should be some general and single structure which can handle such things like Async call, HttpGet and HttpPost and parsing stuff. So I created this simple structure.

Now my question is, what changes and improvements I can add to make it work better and handle such different use cases?

TaskResult.class

public class TaskResult {

    public static int CODE_ERROR = 0;
    public static int CODE_SUCCESS = 1;

    public int code = CODE_ERROR;
    public String message = null;
    private Object data = null;

    public Object getResultData() {
        return data;
    }

    public void setResultData(Object data) {
        this.data = data;
    }
}


BaseParser.class

public interface BaseParser {
    public TaskResult parse(InputStream in);
}


ConnectionManager.class

```
public class ConnectionManager {

private ArrayList params;
private ArrayList headers;
private String url;

public static int GET_REQUEST = 0;
public static int POST_REQUEST = 1;

private int mRequestType = POST_REQUEST;

public ConnectionManager() {
params = new ArrayList();
headers = new ArrayList();
}

public void addParam(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}

public void addHeader(String name, String value) {
headers.add(new BasicNameValuePair(name, value));
}

public void setRequestType(int type) {
mRequestType = type;
}

public void setUrl(String url) {
this.url = url;
}

public

Solution

Your idea seems quite feasible, I just had a brief look through and these would be my suggestions:

-
TaskResult class

  • "code" and "message" members should probably be encapsulated via getter/setter with proper namings.



-
ConnectionManager class

  • Depending on your scenario maybe you should consider calling "shutdown" on your mClient member.



  • When creating your queryString you should use String.format() instead of manually concatenating strings.



  • The getHttpInputStream() method could suffer a bit of refactoring, you have common code and behaviour in both if/else branches.



Other then that, imo the code looks okey.

Context

StackExchange Code Review Q#41325, answer score: 3

Revisions (0)

No revisions yet.