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

HTTP Get & Post code with slow performance

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

Problem

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 (securedCon

Solution

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.

Code Snippets

if(protocol.startsWith("https"){
    securedConnection = true;
} else {
    securedConnection = false;
}
securedConnection = protocol.startsWith("https");
securedConnection = connectionUrl.substring(0,5).toLowerCase().startsWith("https");
get = new HttpGet(uri.getPath()+query_string);
post = new HttpPost(uri.getPath());
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;
}

Context

StackExchange Code Review Q#23266, answer score: 5

Revisions (0)

No revisions yet.