Slowness in getting api response using Httpurlconnection

Asked

Viewed 82 times

1

I have a critical problem, when I submit a request to the API, it always takes a long time to receive the reply, I am using gcm too, always arrives at the time the request is sent.

But when the function goes in the API, it already takes, it has already taken more than 2 minutes.

The worst is that sometimes comes fast and in other cases not, the problem is not on the internet, so I noticed also is not on the server, since the restfull client returns quickly the result. The function I’m using on Android is this.

public static String postHttp(String targetURL, String urlParameters)
{
    URL url;
    HttpURLConnection connection = null;
    try {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Language", "en-US");

        connection.setUseCaches(false);
        connection.setDoInput(true);


        //Send request
        OutputStreamWriter w = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        w.write(urlParameters);
        w.flush();
        w.close ();

        //Get Response
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();

    } catch (Exception e) {
        String erro = "Erro de conexao com servidor (Método POST) - " + e.getMessage();
       // UpMotoLog.reportException(erro);
        Log.d("Exception", e.getMessage());
        return null;

    } finally {

        if(connection != null) {
            connection.disconnect();
        }
    }
}
  • It seems to me that the problem may be anything but that function... I think the problem is really one of the ones you listed above.

  • Bro, but outside the app in the Rest client the result is immediate, and the internet is 15 MB. That’s what got me confused, I do not know if there is anything else on android that can harm this connection. Vlw!

  • How did you experience Rest client on android? Don’t forget that wireless and quite different cable.

  • Brother, I have a tread here running an asynctask every second, I mentioned it here to do the tests, and it seems to me it has slowed down; What do you suggest to me in this case?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.