I have coded to fetch the image from amazonaws and show it in the expanded view of notification.below is the code
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
The same code works and showing the image in some devices and not in others at the same time.
Images which are not displaying getting exceptions like as below
java.net.UnknownHostException: Unable to resolve host
"examples3.s3.amazonaws.com": No address associated with hostname
java.net.ConnectException: Failed to connect to
examples3.s3.amazonaws.com/1.2.3.4:5(some ip showing, this is for the only
reference)
Related
I am trying to download images from server to be used as map markers icon in my android app.
This is how I am trying to download the images:
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
And this is how I am calling this method:
String urlbitmap = "https://faro.red/buju/administrar/application/admin/usuarios/" + foto;
Log.d("RES SUVJETC","numProfesionales urlbitmap:"+urlbitmap);
Bitmap postbitmap = getBitmapFromURL(urlbitmap);
For the variable urlbitmap, this is the log output:
https://faro.red/buju/administrar/application/admin/usuarios/blue-user-icon.png
The image is at this URL.
I am getting an exception at line:
InputStream input = connection.getInputStream();
The exception output is this:
19-07-26 14:25:41.984 20497-20497/com.mpidesarrollo.buju E/AsyncHttpRH: User-space exception detected!
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:300)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:839)
at com.android.okhttp.okio.Okio$1.write(Okio.java:81)
at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:60)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1154)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:976)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at com.mpidesarrollo.buju.HomeFragment.getBitmapFromURL(HomeFragment.java:355)
I have the same problem. It solved by changing the url protocol from https to http
I want to get XML data from the web server https://ruralfire.qld.gov.au/bushfirealert/bushfireAlert.xml
However, I can't do it because my codes always have an error "javax.net.ssl.SSLHandshakeException: Connection closed by peer".
And my InputStream is always null, so I can't do anything with it (such as parsing).
I ensure that the problem is in my connection but I don't know how to solve it.
This is my code in connecting to the web server:
private InputStream downloadUrl(String urlString) throws IOException {
URL url;
url = new URL(urlString);
InputStream is = null;
try {
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setDoInput(true);
is = con.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
May you help me to solve this problem ?
Thank you so much.
I'm trying to use HttpURLClient to send some POST data to a server using the HttpRestClient class shown below. When executing
conn.setDoInput(true);
I get
java.lang.IllegalStateException: Already connected
I uninstalled the app, and still get the same error.
In all the example I've seen openConnection is called before setDoInput. If, as its name suggests, openConnection opens a connection, it should never be used before `setDoInput, right? What am I missing?
Maybe at some point it crashed before executing disconnect. Could that be the reason? If so, how can I disconnect the old connection?
public class HttpRestClient {
static public int post(String urlStr, List<NameValuePair> data){
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(data));
writer.flush();
writer.close();
os.close();
InputStream is = conn.getInputStream();
String dude = readIt(is);
return 1;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
finally {
if(conn!=null) conn.disconnect();
}
}
}
This might be due to watches while debugging in your IDE. See this answer.
It happened to me and was hard to discover.
You called both of conn.setDoInput(true); and conn.setDoOutput(true);. Use one of them:
setDoOutput(true) is used for POST and PUT requests.
setDoInput(true) is used for GET request.
The connection you made was confused, it can't decide which request should be used.
In your code:
static public int post(String urlStr, List<NameValuePair> data){
HttpURLConnection conn = null;
System.setProperty("http.keepAlive", "false"); // must be set
try {
...
conn.setDoOutput(true);
conn.setRequestMethod("POST");
// and connect to server, if needed
conn.connect();
...
}
....
It may be a misleading exception. See this defect for Jersey-2 https://java.net/jira/browse/JERSEY-2729
The link has been updated:
https://github.com/javaee/jersey/issues/3001
Basically the issue is jersey was throwing invalid exception. The real issue in my case was that the connection was refused from the server.
I am getting MalformedURLException: Protocol not found, when I am trying to load image from URL sting. That image is displaying in Browser when I paste that URL. But not displaying in ImageView. I've used this code:
public static Bitmap getBitmapFromURL(String src) {
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
Please anybody tell me whats the problem? It will be very helpful to me.
Its a
http://74.208.68.90/webservice/images/image44.png
Your code is OK.
malformedexception comes when there is some problem in your url
try to encode your parameters in url with url encoder and protocall not found shows
http is missing from your url
String url = url +"/" + UrlEncoder.encode(param);
I think it is "src" that may have problem,
I'm using the following code to grab images from the web. It uses Gridview and depending on position, picks a URL from an array. It works but the loading of images is often hit or miss. Almost every time I start the app, a different number of images load.
It also has issues when changing from portrait to landscape view, 5 out of 10 images may be displayed then I'll turn the device and usually lose all the images. Sometimes a few do show up though.
Any ideas on making this more robust?
try {
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
return bm;
} catch (IOException e) {
Log.d("DEBUGTAG", "error...");
}
return null;
One thing I read is that there's a known bug with decoding Bitmaps from an InputStream, and the suggested fix from Google was to use a FlushedInputStream (example in below URL):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Also, I'd put the download code into an AsyncTask. Here's what I currently use for mine:
public static Bitmap loadImageFromUri(URI uri)
{
URL url;
try {
url = uri.toURL();
} catch (MalformedURLException e) {
Log.v("URL Exception", "MalformedURLException");
return null;
}
try
{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(new FlushedInputStream(input));
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
I just pass in a URI due to the way I've got the rest of my code set up, you could pass in a URL instead and skip the first part. This will keep the download from tying up your UI thread.