Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Get using Android HttpURLConnection

I'm new to Java and Android development and try to create a simple app which should contact a web server and add some data to a database using a http get.

When I do the call using the web browser in my computer it works just fine. However, when I do the call running the app in the Android emulator no data is added.

I have added Internet permission to the app's manifest. Logcat does not report any problems.

Can anyone help me to figure out what's wrong?

Here is the source code:

package com.example.httptest;  import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView;  public class HttpTestActivity extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          TextView tv = new TextView(this);         setContentView(tv);          try {             URL url = new URL("http://www.mysite.se/index.asp?data=99");             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();             urlConnection.disconnect();             tv.setText("Hello!");         }         catch (MalformedURLException ex) {             Log.e("httptest",Log.getStackTraceString(ex));          }         catch (IOException ex) {             Log.e("httptest",Log.getStackTraceString(ex));         }        }         } 
like image 846
user1119112 Avatar asked Dec 28 '11 10:12

user1119112


People also ask

Can I use HttpURLConnection for https?

HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.

What is HTTP URL connection in Android?

HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods ( OPTIONS , HEAD , PUT , DELETE and TRACE ) can be used with setRequestMethod(String) .

Is HttpURLConnection deprecated?

Deprecated. it is misplaced and shouldn't have existed. HTTP Status-Code 401: Unauthorized. HTTP Status-Code 503: Service Unavailable.


1 Answers

Try getting the input stream from this you can then get the text data as so:-

    URL url;     HttpURLConnection urlConnection = null;     try {         url = new URL("http://www.mysite.se/index.asp?data=99");          urlConnection = (HttpURLConnection) url                 .openConnection();          InputStream in = urlConnection.getInputStream();          InputStreamReader isw = new InputStreamReader(in);          int data = isw.read();         while (data != -1) {             char current = (char) data;             data = isw.read();             System.out.print(current);         }     } catch (Exception e) {         e.printStackTrace();     } finally {         if (urlConnection != null) {             urlConnection.disconnect();         }         } 

You can probably use other inputstream readers such as buffered reader also.

The problem is that when you open the connection - it does not 'pull' any data.

like image 102
Davos555 Avatar answered Sep 30 '22 02:09

Davos555