title: What is the reason of this error java.io.IOException: Content-Length and stream length disagree
body: <p>I am getting this error</p>
  
  <pre><code>   java.io.IOException: Content-Length and stream length disagree
  </code></pre>
  
  <p>on this line of code       <code>return response.body().bytes();</code></p>
  
  <p>this is full code </p>
  
  <p>edit: after some google the reason of the error is from <a href="https://github.com/square/okhttp/blob/53ba4dd9efcb0b469e86eca9d865e64cce950ad4/okhttp/src/main/java/okhttp3/ResponseBody.kt#L133" rel="nofollow noreferrer">okhttp lib</a></p>
  
  <pre><code>if (contentLength != -1L &amp;&amp; contentLength != bytes.size.toLong()) {
    throw IOException(
        "Content-Length ($contentLength) and stream length (${bytes.size}) disagree")
  }
  </code></pre>
  
  <p>but how to fix that ?</p>
  
  <p>edit:</p>
  
  <p><a href="https://i.stack.imgur.com/JYtay.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JYtay.jpg" alt="enter image description here"></a></p>
  
  <p>this is full code:</p>
  
  <pre><code>public class OkHttpHandler extends AsyncTask&lt;Void, Void, byte[]&gt; {
  
      private final String Fetch_URL = "http://justedhak.comlu.com/get-data.php";
      ArrayList&lt;Listitem&gt; Listitem;
      int resulta;
  
      OkHttpClient httpClient = new OkHttpClient();
  
      String myJSON;
      JSONArray peoples = null;
      InputStream inputStream = null;
  
      @Override
      protected byte[] doInBackground(Void... params) {
          Log.d("e", "dddddddddd");
          Log.d("e", Fetch_URL);
  
          Request.Builder builder = new Request.Builder();
          builder.url(Fetch_URL);
  
          Request request = builder.build();
  
          String result = null;
          try {
  
              Response response = httpClient.newCall(request).execute();
            //  int statusCode = response.getStatusLine().getStatusCode();
  
                int statusCode =200;
  
             // HttpEntity entity = response.body().byteStream();
              if (statusCode == 200) {
                  byte[] buffer = new byte[8192];
                  int bytesRead;
                  ByteArrayOutputStream output = new ByteArrayOutputStream();
                  while ((bytesRead = inputStream.read(buffer)) != -1) {
                      output.write(buffer, 0, bytesRead);
              //    inputStream = response.body().byteStream();
  
                  // json is UTF-8 by default
               //   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
              /*    StringBuilder sb = new StringBuilder();
  
                  String line = null;
                  while ((line = reader.readLine()) != null)
                  {
                      sb.append(line + "\n");
                  }
                  result = sb.toString();*/
                  resulta = 1; //"Success
                  Log.d("e", "response.");
                  return response.body().bytes();
  
              }
              else
              {
                  resulta = 0; //"Failed
  
              }
          } catch (Exception e) {
  
              Log.d("e", "r2r2 error");
  
              e.printStackTrace();        }
          finally {
              try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
          }
          return null;
      }
  
      protected void onPostExecute(String result){
          if( resulta ==1){
              myJSON=result;
              showList();
          }
          else{
              Log.e("d","zzzzzzzz");
  
          }
      }
  
      protected void showList(){
          try {
              Log.e("d","jjjjjjjjjj");
  
              JSONObject jsonObj = new JSONObject(myJSON);
              peoples = jsonObj.getJSONArray("result");
              Listitem = new ArrayList&lt;Listitem&gt;();
              for(int i=0;i&lt;peoples.length();i++){
                  JSONObject c = peoples.getJSONObject(i);
                  String id = c.getString("id");
                  String url = c.getString("path");
                  Listitem.add(new Listitem(id,url));
                  Log.e("d","ppppp");
              }
  
           //   GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
              //   gridView.setAdapter(gridAdapter);
             // adapter.notifyDataSetChanged();
  
              //  list.setAdapter(adapter);
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>