0

I'm catching a device event, and based on info coming from it it I need to do a login. If the login is verified, I want to move to the next page of my application.

I tried doing it with HttpURLConnection but it doesn't actually execute the servlet.

This is the genetic code I'm using:

public static String getResponseFromUrl(String url) throws Exception {
    URL website = new URL(url);

    HttpURLConnection connection = (HttpURLConnection)website.openConnection();
    connection.setRequestMethod("GET"); 
    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    connection.setUseCaches(false);

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String response = "";
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        response += inputLine;

    in.close();

    return response.toString();
}

I also tried

InputStream responseInputStream = connection.getInputStream();

but with the same result. The doGet method is not being called, and obviously the app doesn't move to the next page.

I'm pretty sure this can be done by catching the event inside the JSP page but I was hoping there's an easier solution.

EDIT: When submitting the same URL in the browser it all works. The doGet is called and the app moves to the next page.

Eddy
  • 3,075
  • 12
  • 48
  • 75

1 Answers1

-1

From the Doc :

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }

Try with "readStream(in);"

If it still doesn works, the error must be from the URL. u say u have a doGet method at this url, try with your browser to be sure the URL reponds. Do you really need a different doGet/doPost ? use "service" method instead

MeGoodGuy
  • 302
  • 1
  • 9