4
\$\begingroup\$

I had developed an application in which I hit several URLs and show data on mobile. But the problem is that it requires more time. If I check the same URL on Firebug tool I get a response in 2-3 seconds, but the same URL requires 8-15 seconds on mobile. In my Android application I am using HTTP get and post method as follows:

public InputStream httpGetResponseStream(String url, String request) {
connectionUrl = url;
InputStream response = null;
try {
    processGetRequest();
    HttpResponse httpresponse = httpclient.execute(host, get);
    response = httpresponse.getEntity().getContent();
} catch (Exception e) {
    response = null;
}
return response;
}

public InputStream httpPostResponseStream(String url, String request) {
connectionUrl = url;
InputStream response = null;
try {
    processPostRequest();
    if (request != null) {
        InputStreamEntity streamEntity = new InputStreamEntity(
                new ByteArrayInputStream(request.getBytes()), request
                        .length());
        post.setEntity(streamEntity);
    }
    HttpResponse httpresponse = httpclient.execute(host, post);
    response = httpresponse.getEntity().getContent();
}catch (Exception e) {
    response = null;
}
return response;
}

private void processGetRequest(){
    uri = Uri.parse(connectionUrl);
    String protocol = connectionUrl.substring(0, 5).toLowerCase();
    if (protocol.startsWith("https")) {
        securedConnection = true;
    } else {
        securedConnection = false;
    }
    if (securedConnection) {
        host = new HttpHost(uri.getHost(), 443, uri.getScheme());
    } else {
        host = new HttpHost(uri.getHost(), 80, uri.getScheme());
    }
    get = new HttpGet(uri.getPath()+query_string);
}

private void processPostRequest() {
uri = Uri.parse(connectionUrl);
String protocol = connectionUrl.substring(0, 5).toLowerCase();
if (protocol.startsWith("https")) {
    securedConnection = true;
} else {
    securedConnection = false;
}
if (securedConnection) {
    host = new HttpHost(uri.getHost(), 443, uri.getScheme());
} else {
    host = new HttpHost(uri.getHost(), 80, uri.getScheme());
}
post = new HttpPost(uri.getPath());
}

Any suggestion to improve code will be appreciated

\$\endgroup\$
  • \$\begingroup\$ Without inspecting the code: Could it be that your mobile is slower in bandwidth and hardware processing power? \$\endgroup\$ – tb- Feb 28 '13 at 13:54
  • \$\begingroup\$ @tb- thanks for suggestion yes, mobile is slower in bandwidth and hardware processing power but still there is any change that I should make in code so speed will increase. \$\endgroup\$ – PPD Mar 1 '13 at 4:14
  • \$\begingroup\$ Try using HttpUrlConnection instead of HttpClient. Android's HTTP clients \$\endgroup\$ – vasart Mar 1 '13 at 12:01
5
\$\begingroup\$

I personally haven't used anything HTTP-related in Java yet, and thus can't talk about your performance. What I can see though is the following code:

if(protocol.startsWith("https"){
    securedConnection = true;
} else {
    securedConnection = false;
}

I will assume you have securedConnection as a private classwide field, so I won't talk about the fact that you only use it to determine the port in the method scope.

the fun thing is, your code could be written in one line:

securedConnection = protocol.startsWith("https");

if you now inline protocol (Eclipse Alt + Shift + i) you get following line:

securedConnection = connectionUrl.substring(0,5).toLowerCase().startsWith("https");

you see that the call to substring is unneccesary.


You also have a large duplication in itself with the methods processPostRequest() and processGetRequest. they are actually exactly the same apart from the last line:

get = new HttpGet(uri.getPath()+query_string);
post = new HttpPost(uri.getPath()); 

you might want to move them into a method like this:

private enum Type{
    GET, POST
}

private HttpRequest processRequestOfType(Type t){
   uri = Uri.parse(connectionUrl);
   securedConnection = connectionUrl.toLowerCase().startsWith("https");
   host = securedConnection ? new HttpHost(uri.getHost(), 443, uri.getScheme())
              : new HttpHost(uri.getHost(), 80, uri.getScheme());
   request = t == Type.GET ? new HttpGet(uri.getPath()+query_string) 
              : new HttpPost(uri.getPath());
   return request;
}

Lastly I saw that in your method httpGetResponseStream() you have the parameter request, which is never used in the code you posted.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.