httpurlconnection

getContentLength() returning -1 on some devices and not others

早过忘川 提交于 2019-12-18 07:20:51
问题 I'm trying to obtain the size of a file before I download it. I use conn.getContentLength(); to do this and it works fine on my home computers Android 2.1 Emulator. It however doesn't work once I run my app from my phone (either WiFi or 3G) and it also doesn't work when I run it from my work laptops Android 2.1 Emulator. Does anyone know a workaround for this? Is there another way I can obtain the size of the file maybe without using HttpURLConnection . 回答1: This information will not always

Sending json object via http post method in android

不羁岁月 提交于 2019-12-18 05:23:14
问题 its NOT DUPLICATE.Link that has been provided is an OLD one."http client" has been removed in api23 I want to send json object: {"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"} to url: http://10digimr.mobimedia.in/api/mobile_retailer/update_profile How do i do it? via post method? METHOD: POST /api/mobile_retailer/update_profile

HttpURLConnection timeout defaults

青春壹個敷衍的年華 提交于 2019-12-18 04:05:13
问题 I seem to be having trouble with some tcp requests getting "stuck" at times, like it is waiting for some response but the connection has been "severed" so a response will never come. Is this expected behavior for HttpURLConnection with default timeouts? Are there sensible defaults set so that I can't get into this odd "hung" situation by default? 回答1: Appears the "default" timeouts for HttpURLConnection are zero which means "no timeout." Unfortunately, in my experience, it appears using these

How to upload binary file using URLConnection

我只是一个虾纸丫 提交于 2019-12-18 04:02:28
问题 In order to upload a binary file to an URL, I have been advised to use this guide. However, the file is not in a directory, but is stored in a BLOB field in MySql db. The BLOB field is mapped as a byte[] property in JPA: byte[] binaryFile; I have slightly modified the code taken from the guide, in this way: HttpURLConnection connection = (HttpURLConnection ) new URL(url).openConnection(); // set some connection properties OutputStream output = connection.getOutputStream(); PrintWriter writer

How to send an image from Android client to Node.js server via HttpUrlConnection?

流过昼夜 提交于 2019-12-18 03:46:42
问题 I am trying to send an image to the server using HttpUrlConnection, because it's recommended by Google. I decided to convert the image into Base64 string and send it to the server where I decoded it into .jpg file. But this method is only feasible with small-sized thumbnails and I cannot send a full-sized images. Here is the android client code: public static void postData(Bitmap imageToSend) { try { URL url = new URL("http://"); HttpURLConnection conn = (HttpURLConnection) url.openConnection

How to overwrite http-header “Host” in a HttpURLConnection?

无人久伴 提交于 2019-12-18 03:38:30
问题 My code is like the following: URL url = new URL("1.0.0.25/otfg/services"); HttpURLConnection cnx = url.openConnection(); cnx.setRequestProperty("Host", "example.org"); But when I log the outgoing packages with tcpdump the http-header "Host" is 1.0.0.25. Could it be that the http-header "Host" is overwritten at a later stage of the sending process, if yes how can I avoid this behavior. 回答1: Duplicate question. The last comment solved my problem: System.setProperty("sun.net.http

What could cause socket ConnectException: Connection timed out?

半城伤御伤魂 提交于 2019-12-17 23:15:13
问题 We have a Webstart client that communicates to the server by sending serialized objects over HTTPS using java.net.HttpsURLConnection . Everything works perfectly fine on my local machine and on test servers located in our office, but I'm experiencing a very, very strange issue which is only occurring on our production and staging servers (and sporadically at that). The main difference I know of between those servers and the ones in our office is that they are located elsewhere and client

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

隐身守侯 提交于 2019-12-17 21:17:00
问题 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

POST a file with other form data using HttpURLConnection

走远了吗. 提交于 2019-12-17 18:44:23
问题 Im trying to POST a file to an API of mine along with other parameters. Eg. POST /media with the parameters filename = 'test.png' file = -the-actual-file- I can do this successfully with Postman (using form-data), so the api side of things are fine. Here is my android code using HttpURLConnection: nameValuePairs.add(new BasicNameValuePair("filename", "test.png")); URL object = new URL(url); HttpURLConnection connection = (HttpURLConnection) object.openConnection(); connection.setReadTimeout

Digest authentication in Android using HttpURLConnection

一个人想着一个人 提交于 2019-12-17 13:38:07
问题 as the question allready says, I am trying to do digest authentication in android. Until now i have used the DefaultHttpClient and it's authentication method (using UsernamePasswordCredentials and so on), but it is deprecated since Android 5 and will be removed in Android 6. So i am about to switch from DefaultHttpClient to HttpUrlConnection . Now i am trying to achieve digest authentication, which should work pretty simple as explained here: Authenticator.setDefault(new Authenticator() {