httpurlconnection

AndroidHttpClient versus HttpUrlConnection for API Level 9 and above

三世轮回 提交于 2019-11-30 22:46:36
问题 Which is better for API Level 9(Gingerbread) and why? Is there any benefit to use AndroidHttpClient over HttpUrlConnection? Any help or guidance will be well appreciated. 回答1: Google doesn't recommend using AndroidHttpClient, only on very old Android versions. But as you can see in many questions on SO, HttpURLConnection is not easy to use. It's very low-level and doesn't have a comfortable interface. You have to know quite a lot how it's working internally and if you have luck, you get

Reading HttpURLConnection InputStream - manual buffer or BufferedInputStream?

随声附和 提交于 2019-11-30 22:06:57
When reading the InputStream of an HttpURLConnection, is there any reason to use one of the following over the other? I've seen both used in examples. Manual Buffer: while ((length = inputStream.read(buffer)) > 0) { os.write(buf, 0, ret); } BufferedInputStream is = http.getInputStream(); bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append(current); } EDIT I'm still new to HTTP in general but one consideration that comes to mind is that if I am using a persistent HTTP connection, I can't just read

Retrieve the final location of a given URL in Java

元气小坏坏 提交于 2019-11-30 21:46:48
I am trying to retrieve the final location of a given URL (String ref) as follows: HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection(); con.setInstanceFollowRedirects(true); con.setRequestProperty("User-Agent",""); int responseCode = con.getResponseCode(); return con.getURL().toString(); It works in most cases, but rarely returns a URL which yet contains another redirection. What am I doing wrong here? Why do I get responseCode = 3xx, even after calling setInstanceFollowRedirects(true)? UPDATE: OK, responseCode can sometimes be 3xx. If it happens, then I will return con

Reading HttpURLConnection InputStream - manual buffer or BufferedInputStream?

谁说我不能喝 提交于 2019-11-30 17:32:27
问题 When reading the InputStream of an HttpURLConnection, is there any reason to use one of the following over the other? I've seen both used in examples. Manual Buffer: while ((length = inputStream.read(buffer)) > 0) { os.write(buf, 0, ret); } BufferedInputStream is = http.getInputStream(); bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append(current); } EDIT I'm still new to HTTP in general but one

Parse Exception: At line 1, column 0: no element found

半城伤御伤魂 提交于 2019-11-30 17:03:23
问题 I have a weird issue. I receive the following error that causes a force-close: org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:508) at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467) at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329) at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286) After clicking the Force Close button, the

Android HttpURLConnection: Handle HTTP redirects

非 Y 不嫁゛ 提交于 2019-11-30 14:00:03
问题 I'm using HttpURLConnection to retrieve an URL just like that: URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(true); // ... I now want to find out if there was a redirect and if it was a permanent (301) or temporary (302) one in order to update the URL in the database in the first case but not in the second one. Is this possible while still using the redirect handling of HttpURLConnection and if, how?

Why timeout value is not respected by android HttpURLConnection?

北城以北 提交于 2019-11-30 13:45:52
问题 I have the following code block: try { URL url = new URL("http://site-to-test.com/nonexistingpage.html"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("User-Agent", CoreProtocolPNames.USER_AGENT); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(500); // timeout is in milliseconds urlc.connect(); if (urlc.getResponseCode() == 404) { // Server was reachable Log.i(TAG, "Server is reachable"); } } catch (MalformedURLException

Passing JSON data in get request as request body

孤街醉人 提交于 2019-11-30 12:43:05
Hi i have to send a get request to an url http://onemoredemo.appspot.com/group?authToken=access_token&authMethod=oauth with request body contains json object as shown below. {"goupid":"some_variable" } Here is a section of java code for sending get request: URL url1=new URL("http://onemoredemo.appspot.com/group?authToken="+access_token+"&authMethod=oauth"); conn=(HttpURLConnection) url1.openConnection(); conn.addRequestProperty("Content-type", "application/x-www-form-urlencoded"); conn.setRequestMethod("GET"); conn.setDoOutput(true); JSONObject jj=new JSONObject(); HttpGet get; get. jj.put(

Send data in Request body using HttpURLConnection

依然范特西╮ 提交于 2019-11-30 12:41:49
I am using HttpURLConnection to make a POST request to a local service deployed in my local created using JAVA Spark. I want to send some data in request body when I make the POST call using the HttpURLConnection but every time the request body in JAVA Spark is null . Below is the code I am using for this Java Spark POST Service Handler post("/", (req, res) -> { System.out.println("Request Body: " + req.body()); return "Hello!!!!"; }); HTTPClass Making the post call `public class HTTPClassExample{ public static void main(String[] args) { try{ URL url = new URL("http://localhost:4567/");

Android (Java) HttpURLConnection silent retry on 'read' timeout

﹥>﹥吖頭↗ 提交于 2019-11-30 11:07:55
问题 So I'm using Google Volley for HTTP request, which basically uses Java 's HttpURLConnection . According to my tests, the problem is this : When the 'read' timeout on the HttpURLConnection reaches, a silent retry is executed before the connection is closed and the relevant exception thrown ( SocketTimeoutException ). Note that: - I noticed this error when using HTTP POST request. - 'read' timeout is different than 'connect' timeout. - If the 'read' timeout (set by calling connection