Android: Http GET request cannot connect

I am new to Android programming and can really use some help with the program I am writing to establish an Http connection and display an image.

I use Wei Meng Li's book, β€œGetting Started Developing Android Applications.” The code compiles and there are no errors, but every time I run the program, the error message "error message" appears. Image is not displayed.

I have looked at various code samples for GET requests, but cannot find anything that works with my code.

Any help that anyone can offer will be very grateful, as I try my best to see a solution.

The last line of use-permission code was included in the manifest.

ImageView image; private InputStream OpenHttpConnection(String urlString) throws IOException { InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if(!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP Connection"); try { HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex) { throw new IOException("Error connecting"); } return in; } private Bitmap DownloadImage(String URL) { Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show(); e1.printStackTrace(); } return bitmap; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_network); Bitmap bitmap = DownloadImage("http://www.mayoff.com/5-01cablecarDCP01934.jpg"); image = (ImageView) findViewById(R.id.image); image.setImageBitmap(bitmap); } <uses-permission android:name="android.permission.INTERNET" /> 
+4
source share
1 answer

Perhaps the problem is that you have a version of the api. you must use the AsyncTask class to access web functions.

This may be due to the fact that in apis 11 and above access to the network in the main thread is not allowed, you may have to use the ASYNC task.

an example of using the ASYNC task;

 class InternetFileCheack extends AsyncTask<Object, Void, Boolean> { private Button btn; private String fileURL; Context c; public InternetFileCheack (Button imv, String url, Context ctx) { this.btn = imv; this.fileURL = url; this.c = ctx; } @Override protected Boolean doInBackground(Object... params) { Boolean sonuc = null; try { URL u = new URL(fileURL); HttpURLConnection huc = (HttpURLConnection) u.openConnection(); huc.setRequestMethod("HEAD"); huc.connect(); int code = huc.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { sonuc = true; } else { sonuc = false; } } catch (Exception e) { sonuc = false; } return sonuc; } @Override protected void onPostExecute(Boolean result) { btn.setEnabled(result); if (result) { Toast.makeText(c, "", Toast.LENGTH_LONG).show(); btn.setVisibility(View.VISIBLE); } else { btn.setVisibility(View.GONE); } } } 
+2
source

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


All Articles