Http request on Android

I need help sending http get request. Like this:

URL connectURL; connectURL = new URL(address); HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); // do some setup conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("GET"); // connect and flush the request out conn.connect(); conn.getOutputStream().flush(); // now fetch the results String response = getResponse(conn); et.setText(response); 

I searched on the Internet, but any method I try to execute, the code does not work with 'conn.connect();' . Any clues?

+4
source share
2 answers

If you need a demo code , try the following:

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

and this:

  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { urlConnection.setDoOutput(true); urlConnection.setChunkedStreamingMode(0); OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); writeStream(out); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); } finally { urlConnection.disconnect(); } 

Hope this helps.

+3
source

It is very difficult to say without an actual error message. Random thought: Have you added Internet permission to the manifest?

  <uses-permission android:name="android.permission.INTERNET"/> 
+4
source

Source: https://habr.com/ru/post/1334748/


All Articles