title: java.net.SocketException: Connection reset OKHTTP
body: <p>I am using okhttp for uploading file to my server and loading the file on google docs viewer for preview to my users. However, the upload is failing sometimes as I am not able to find the file on the server. The exception raised gives the following stacktrace : </p>
  
  <pre><code> Cannot setInForeground() - never saw a connection for the pid: 22551
  08-08 04:23:37.208 22551-22605/  W/System.err: java.net.SocketException: Connection reset
  08-08 04:23:37.330 22551-22551/  W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551
  08-08 04:23:37.331 22551-22551/  W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551
  08-08 04:23:39.213 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551
  08-08 04:23:40.672 22551-22551/  W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551
  08-08 04:23:41.203 22551-22551/  W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551
  08-08 04:23:52.103 2751-3347/? D/WifiQualifiedNetworkSelector:: Current connection state (true,false,false)
  08-08 04:24:04.811 4124-4124/? W/GmsClientEvents: unregisterConnectionCallbacks(): listener agqt@1598588 not found
  </code></pre>
  
  <p>The PORT 80 is open as well. However, sometimes the upload fails consistently. Most of the time it works fine. Is it an issue on the server or client side? If client side were the reason how does it work certain times. Is the timeout error the cause of the problem? I checked with files big or small but I do not know what causes the problem.</p>
  
  <p>Here is my code for uploading the file :</p>
  
  <pre><code> try {
          File f  = new File(selectedFilePath);
  
          String file_path = f.getAbsolutePath();
          OkHttpClient client = new OkHttpClient();
  
          RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f);
          Log.e("msh", content_type);
          RequestBody request_body = new MultipartBody.Builder()
                  .setType(MultipartBody.FORM)
                  .addFormDataPart("type",content_type)
                  .addFormDataPart("uploaded_file",file_path.substring(file_path.lastIndexOf("/")+1), file_body)
                  .build();
  
          Request request = new Request.Builder()
                  .url("http://fakeurl.com/retrofit_example/save_file.php?neran="+neran)
                  .post(request_body)
                  .build();
  
  
  
              response = client.newCall(request).execute(); //exception is here
  
  
  
          } catch (UnknownHostException e) {
              e.printStackTrace();
              /*uploadFile(selectedFilePath);*/
              upsuccess = false;
          } catch (IOException e) {
              e.printStackTrace();
              upsuccess = false;
          }  catch (NullPointerException e) {
              e.printStackTrace();
              /*uploadFile(selectedFilePath);*/
              upsuccess = false;
          }
  
          if(response!=null) {
              if (response.isSuccessful()) {
                  upsuccess = true;
              }
              response.body().close();
          }
  </code></pre>
  
  <p>I have tried everything discussed in other questions on stackoverflow but the error persists. Although uploading fails with okhttp but other methods and libraries also have the above issue. </p>
  
  <p>Other answers also suggested :</p>
  
  <ol>
  <li>Increase upload file size : my file is just 55 kb and limit is 512 MB</li>
  <li>PHP log doesn't show anything for upload fails</li>
  </ol>
  
  <p><strong>UPDATE</strong> : I am not entirely sure but this issue usually occurs with WIFI sometimes not with Mobile internet. Also I have noticed sometimes turning off wifi and then turning on seems to solve the issue but as of now due to various reasons I do not want to do this on my app programatically because I will need extra permissions. CAN ANYONE TELL ME WHAT CAN BE THE REASON FOR THE ISSUE? </p>