urlconnection

setImageURI / setimagebitmap to fetch image from net and display in image view

萝らか妹 提交于 2019-12-06 07:37:46
I want to fetch image from net from some URL and show it in my ImageView. following is the code i am using :- Bitmap bm = null; try { URL aURL = new URL("stringURL"); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); }catch (Exception e) { // TODO: handle exception } iv.setImageBitmap(bm); but i can not get the image I think the error is in URL aURL = new URL("stringURL"); should be without quotes URL aURL = new URL(stringURL);

Why does HttpURLConnection.getResponseCode() throws IOException? [duplicate]

空扰寡人 提交于 2019-12-05 09:13:42
This question already has an answer here: HttpURLConnection.getResponseCode() throws IOException when code is known 2 answers I can see that getResponseCode() method is just a getter Method that returns the statusCode already set by the a connect action that happened before. So in this context why does it throw an IOException ? Am i missing something? From javadoc : It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP). Returns: the HTTP Status-Code, or -1 Throws: IOException - if an error occurred connecting to

Is it possible to check progress of URLconnection.getInputStream()?

陌路散爱 提交于 2019-12-04 12:12:07
问题 I want to check progress of downloading file by URLconnection. Is it possible or should I use another library? This is my urlconnection function: public static String sendPostRequest(String httpURL, String data) throws UnsupportedEncodingException, MalformedURLException, IOException { URL url = new URL(httpURL); URLConnection conn = url.openConnection(); //conn.addRequestProperty("Content-Type", "text/html; charset=iso-8859-2"); conn.setDoOutput(true); OutputStreamWriter wr = new

Setting custom header using HttpURLConnection

此生再无相见时 提交于 2019-12-04 12:01:41
问题 I am simply making a GET request to a Rest API using HttpURLConnection . I need to add some custom headers but I am getting null while trying to retrieve their values. Code: URL url; try { url = new URL("http://www.example.com/rest/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Set Headers conn.setRequestProperty("CustomHeader", "someValue"); conn.setRequestProperty("accept", "application/json"); // Output is null here <-------- System.out.println(conn

Connection to a URL from within an applet using Apache's HttpClient vs using the JDK's URLConnection

[亡魂溺海] 提交于 2019-12-04 10:50:55
In the following code, I have verified that connecting to a URL from within an applet preserves the browser's session if JDK's URLConnection class is used. However, this is not the case if Apache's HttpClient library is used. Does anyone know why? Alternatively, is there a way for me to set the connection instance to be used by an HttpClient instance? import java.applet.Applet; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import javax.net.ssl.SSLException; import org.apache.http.client

How do I download part of a file using Java?

感情迁移 提交于 2019-12-04 05:53:39
问题 I'm using this Java code to download a file from the Internet: String address = "http://melody.syr.edu/pzhang/publications/AMCIS99_vonDran_Zhang.pdf"; URL url = new URL(address); System.out.println("Opening connection to " + address + "..."); URLConnection urlC = url.openConnection(); urlC.setRequestProperty("User-Agent", ""); urlC.connect(); InputStream is = urlC.getInputStream(); FileOutputStream fos = null; fos = new FileOutputStream("myFileName"); int oneChar, count = 0; while ((oneChar =

How do i do the following curl command in Java

对着背影说爱祢 提交于 2019-12-04 04:21:12
问题 How would I implement the following curl command in Java URLConnection curl -X PUT \ -H "X-Parse-Application-Id: " \ -H "X-Parse-REST-API-Key: " \ -H "Content-Type: application/json" \ -d '{"score":73453}' Thanks in advance 回答1: Using the derived class of URLConnection which is HttpURLConnection you can easily do it. URL myURL = new URL(serviceURL); HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection(); myURLConnection.setRequestMethod("PUT"); myURLConnection

How to parse an application/json object into a String

烂漫一生 提交于 2019-12-04 03:57:05
问题 I am programmatically navigating to a site that returns application/json format. I can't seem to read the json returned in the HttpURLConnection. I am using Jackson to demarshall the JSON into the java object. The code is: InputStreamReader isr = new InputStreamReader(connection.getInputStream()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); line = br.readLine(); } geoLocation = (new

Java ssl performance issue in connection with downloads

牧云@^-^@ 提交于 2019-12-03 21:30:54
i'm currently working on a small swift-client and got some hard performance issues by downloading files. After thousands of checks i localized the problem by downloading files through ssl. Normal downloads(http) works fine without any problems(or performance issues). But SSL downloads blow up my CPU ... a single core goes up to 100 % load (for a single thread) i wrote a small testclass without my entire program and can confirm my previous observations. package testDownload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java

Can I globally set the timeout of HTTP connections?

会有一股神秘感。 提交于 2019-12-03 15:26:13
问题 I have a program that uses javax.xml.ws.Service to call a remote service defined by a WSDL. This program runs on the Google App Engine which, by default, sets the HTTP connection timeout to 5 seconds{1}. I need to increase this timeout value since this service often takes a long time to respond, but since this request is not being made with URLConnection , I cannot figure out how to call URLConnection.setReadTimeout(int) {2}, or otherwise change the timeout. Is there any way to globally set