httpurlconnection

Send File And Parameters To Server With HttpURLConnection in android API 23

隐身守侯 提交于 2019-11-28 14:25:56
After many tries I Solved it, there is the code i use to send parameters and image : public class PurchaseAsync extends AsyncTask<String, Void, Boolean> { public static final String TAG = PurchaseAsync.class.getSimpleName(); public PurchaseAsync(ArrayList<CustomItem> parameters, String imageAddress, PurchaseListener listener){ this.parameters = parameters; this.imageAddress = imageAddress; this.listener = listener; if(this.parameters == null){ this.parameters = new ArrayList<>(); } LTH.dLog(WMH.WEBSERVICE, TAG + " -> Image path : " + imageAddress); } private String imageAddress = ""; // ======

Android FileNotFound Exception - Cannot getInputStream from image URL that does not have file format

蓝咒 提交于 2019-11-28 12:44:00
The title is pretty self explanatory. the following code...: URL imageUrl = new URL(url); try { HttpURLConnection conn= (HttpURLConnection)imageUrl.openConnection(); conn.setDoInput(true); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); return BitmapFactory.decodeStream(is); } catch (IOException e) { e.printStackTrace(); } Will fail if the url does not contain the file format. For example, some images hosted by google will display the image yet not have the file format (.png, .jpg, etc) as part of the url. As a result, the content-type header of

How to switch from HttpClient to HttpUrlConnection?

偶尔善良 提交于 2019-11-28 12:40:21
I am creating an Android application and I send data from Android application to servlet through HttpClient. I use HttpPost method. I read in Android developer site that Apache HttpClient library has some bug in Android Froyo 2.2 and after all it's good practice to use HttpUrlConnection instead HttpPost. So I want to convert my HttpPost code to HttpUrlConnectio but don't know how. I am posting my Android code as well as servlet code here Android code private String postData(String valueIWantToSend[]) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient();

HTTP Basic Authentication issue on Android Jelly Bean 4.1 using HttpURLConnection

妖精的绣舞 提交于 2019-11-28 12:23:37
We are making HttpURLConnection based request to the Web server using HTTP Basic Authentication. Code works great on Android versions 2.x, 3.x., 4.0.x Now with Jelly Bean and v4.1.x authentication fails with the following messages in the LogCat: 01-27 10:54:18.886: ...::doReadRawData(731): An exception occured while reading data from remote host. httpURLConn.responseCode = 401 / httpURLConn.responseMessage = UNAUTHORIZED 01-27 10:54:18.886: ...::doReadRawData(731): java.io.IOException: No authentication challenges found The Authentication code we using for HttpURLConnection as in the Android

FileDownloader at the android it is showing no errors but the download does not start

蹲街弑〆低调 提交于 2019-11-28 11:42:24
问题 I am trying to download a simple .pdf file I have tried logs but no errors and nothing. I tried to debug but the same till yesterday it worked like charm but not today, I didn't change nothing at this part of the code. I am based at this question and answer at SO. https://stackoverflow.com/a/24748227/9560126 Below is the code. Sometimes at the LogCat it shows me only like message something like this. com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl

Java URLConnection - When do I need to use the connect() method?

北战南征 提交于 2019-11-28 09:37:20
I have a problem to understand the meaning of the connect() method in the URLConnection class. In the following code, if I use the connect() method, I get the same result if I don't use it. Why (or when) do I need to use it? URL u = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect();//with or without it I have the same result InputStream in = conn.getInputStream(); int b; while ((b = in.read()) != -1) { System.out.write(b); } Amanda HttpURLConnection conn = (HttpURLConnection) u.openConnection(); only creates an Object connect()

HttpURLConnection implementation

扶醉桌前 提交于 2019-11-28 08:20:57
I have read that HttpURLConnection supports persistent connections, so that a connection can be reused for multiple requests. I tried it and the only way to send a second POST was by calling openConnection for a second time. Otherwise I got a IllegalStateException("Already connected"); I used the following: try{ URL url = new URL("http://someconection.com"); } catch(Exception e){} HttpURLConnection con = (HttpURLConnection) url.openConnection(); //set output, input etc //send POST //Receive response //Read whole response //close input stream con.disconnect();//have also tested commenting this

Java HttpURLConnection.getInputStream but get 401 IOException

落爺英雄遲暮 提交于 2019-11-28 07:34:01
问题 I am writing a REST client for CouchDB in Java. The following code should be quite standard: this.httpCnt.connect(); Map<String, String> responseHeaders = new HashMap<>(); int i = 1; while (true){ String headerKey = this.httpCnt.getHeaderFieldKey(i); if (headerKey == null) break; responseHeaders.put(headerKey, this.httpCnt.getHeaderField(i)); i++; } InputStreamReader reader = new InputStreamReader(this.httpCnt.getInputStream()); StringBuilder responseBuilder = new StringBuilder(); char[]

Where does the socket timeout of 21000 ms come from?

二次信任 提交于 2019-11-28 05:57:45
问题 The Problem An app I'm maintaining keeps getting socket timeouts after approximately 21000 ms, despite the fact that I've explicitly set longer timeouts. This seemingly magical value of 21000 ms has come up in a few other SO questions and answers, and I'm trying to figure out exactly where it comes from. Here's the essence of my code: HttpURLConnection connection = null; try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout

In Java, how close the connection and free the port/socket using HttpURLConnection?

孤人 提交于 2019-11-28 05:11:18
问题 I'm using HttpURLConnection to do download of pages in Java. I forgot to release the connections and I think this was causing some problems to my system (a webcrawler). Now I've done some tests and see that after disconnecting, some connections still like TIME_WAIT in the results from the netstat command on Windows. How I do to free this connection immediately? Example code: private HttpURLConnection connection; boolean openConnection(String url) { try { URL urlDownload = new URL(url);