6

I am using HttpURLConnection to upload an image and get its response.
It works on emulator and my XiaoMi device.
However, it always get a SocketTimeoutException on my Sony device on the line connection.getInputStream().
I've tried to set timeouts to large value like 1 minute but not work.

 public String uploadFile(File file, String requestURL) {

        if (file != null) {
            long fileSize = file.length();
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection connection = null;
            try {
                //config of connection
                connection = (HttpURLConnection) new URL(requestURL).openConnection();
                connection.setConnectTimeout(10000);
                connection.setReadTimeout(10000);
                connection.setRequestMethod("PUT");
                connection.setRequestProperty("Content-Type", "image/jpeg");
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-length", "" + fileSize);
                connection.connect();

                //upload file
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                int bytesRead;
                byte buf[] = new byte[1024];
                BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
                while ((bytesRead = bufInput.read(buf)) != -1) {
                    out.write(buf, 0, bytesRead);
                    out.flush();
                }
                out.flush();
                out.close();

                //get response message, but SocketTimeoutException occurs here
                BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
                StringBuilder sb = new StringBuilder();
                String output;
                while ((output = br.readLine()) != null) {
                    sb.append(output);
                }

                //return response message
                return output;

            } catch (Exception e) {
                // Exception
                e.printStackTrace();
            } finally {
                if (connection != null) connection.disconnect();
            }
        }

        return null;
    }

What causes this problem happen?And how to fix it?

Additional Info: I tested on devices under same wifi connection. And sure network and file server worked properly. The file size of tested images is about 100~200kbyte.

halfer
  • 19,824
  • 17
  • 99
  • 186
Season
  • 1,178
  • 2
  • 22
  • 42

2 Answers2

0

Because you set a read timeout of ten seconds and no response was received within ten seconds.

Is this a trick question?

NB You don't need to set the content-length header. Java will do that for you.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Just remove connection.setReadTimeout() statement because by default it will set readTiomeout value to 0 i.e it will wait for data until data is available.so,you might not get SocketTimeOut Exception.