Http Get Android Support HttpURLConnection

I am new to Java and Android development and trying to create a simple application that needs to contact a web server and add some data to the database using http get.

When I make a call using a web browser on my computer, it works fine. However, when I make a call launching the application in the Android emulator, the data is not added.

I have added Internet permission to the application manifest. Logcat does not report any problems.

Can someone help me figure out what happened?

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)); } } } 
+43
java android-emulator android-internet
Dec 28 '11 at 10:47
source share
5 answers

Try to get input from this, you can get text data like this: -

  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(); } } 

Perhaps you can also use other input readers, such as a buffered reader.

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

+53
Dec 28 '11 at 11:03
source
β€” -

Here is the full AsyncTask class

 public class GetMethodDemo extends AsyncTask<String , Void ,String> { String server_response; @Override protected String doInBackground(String... strings) { URL url; HttpURLConnection urlConnection = null; try { url = new URL(strings[0]); urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK){ server_response = readStream(urlConnection.getInputStream()); Log.v("CatalogClient", server_response); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.e("Response", "" + server_response); } } // Converting InputStream to String private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer response = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { response.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return response.toString(); } 

To call this AsyncTask class

 new GetMethodDemo().execute("your web-service url"); 
+22
Jul 11 '16 at 18:03
source

I created an Activity class with a callBack (delegate) response.

 public class WebService extends AsyncTask<String, Void, String> { private Context mContext; private OnTaskDoneListener onTaskDoneListener; private String urlStr = ""; public WebService(Context context, String url, OnTaskDoneListener onTaskDoneListener) { this.mContext = context; this.urlStr = url; this.onTaskDoneListener = onTaskDoneListener; } @Override protected String doInBackground(String... params) { try { URL mUrl = new URL(urlStr); HttpURLConnection httpConnection = (HttpURLConnection) mUrl.openConnection(); httpConnection.setRequestMethod("GET"); httpConnection.setRequestProperty("Content-length", "0"); httpConnection.setUseCaches(false); httpConnection.setAllowUserInteraction(false); httpConnection.setConnectTimeout(100000); httpConnection.setReadTimeout(100000); httpConnection.connect(); int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); } } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (onTaskDoneListener != null && s != null) { onTaskDoneListener.onTaskDone(s); } else onTaskDoneListener.onError(); } } 

Where

 public interface OnTaskDoneListener { void onTaskDone(String responseData); void onError(); } 

You can change according to your needs. This is for getting

+7
Aug 31 '16 at 6:33
source

If you just need a very simple call, you can directly use the URL:

 import java.net.URL; new URL("http://wheredatapp.com").openStream(); 
+3
Sep 19 '15 at 16:27
source

URL url = new URL (" https://www.google.com ");

// if you use

URLConnection conn = url.openConnection ();

// change it to

HttpURLConnection conn = (HttpURLConnection) url.openConnection ();

-3
Nov 02 '16 at 8:06
source



All Articles