title: Unexpected End of stream retrofit while receiving response from esp8266
body: <p>The Arduino Code is:</p>
  
  <pre><code>#define ASCII_0 48
  #define ESP8266 Serial3
  
  String SSID = "baghairat";
  String PASSWORD = "abc12345";
  int packet_len;
  char *pb;
  
  int LED = 13;
  
  boolean FAIL_8266 = false;
  
  String strHTML1 = "&lt;!doctype html&gt;\
  &lt;html&gt;\
  &lt;body&gt;\
  ";
  
  String strHTML2 = "&lt;/body&gt;\
  &lt;/html&gt;";
  
  //String strHTML = "arduino-er.blogspot.com";
  
  #define BUFFER_SIZE 128
  char buffer[BUFFER_SIZE];
  
  void setup() {
  pinMode(LED, OUTPUT);
  
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  
  do{
  Serial.begin(115200);
  ESP8266.begin(115200);
  
  //Wait Serial Monitor to start
  while(!Serial);
  Serial.println("--- Start ---");
  
  ESP8266.println("AT+RST");
  delay(1000);
  if(ESP8266.find("ready"))
  {
    Serial.println("Module is ready");
  
    ESP8266.println("AT+GMR");
    delay(1000);
    clearESP8266SerialBuffer();
  
    ESP8266.println("AT+CWMODE=1");
    delay(2000);
  
    //Quit existing AP, for demo
    Serial.println("Quit AP");
    ESP8266.println("AT+CWQAP");
    delay(1000);
  
    clearESP8266SerialBuffer();
    if(cwJoinAP())
    {
      Serial.println("CWJAP Success");
      FAIL_8266 = false;
  
      delay(3000);
      clearESP8266SerialBuffer();
      //Get and display my IP
      sendESP8266Cmdln("AT+CIFSR", 1000);  
      //Set multi connections
      sendESP8266Cmdln("AT+CIPMUX=1", 1000);
      //Setup web server on port 80
      sendESP8266Cmdln("AT+CIPSERVER=1,80",1000);
  
      Serial.println("Server setup finish");
    }else{
      Serial.println("CWJAP Fail");
      delay(500);
      FAIL_8266 = true;
    }
   }else{
    Serial.println("Module have no response.");
    delay(500);
    FAIL_8266 = true;
  }
  }while(FAIL_8266);
  
  digitalWrite(LED, HIGH);
  
  //set timeout duration ESP8266.readBytesUntil
  ESP8266.setTimeout(1000);
  }
  
  void loop(){
  int connectionId;
  
  if(ESP8266.readBytesUntil('\n', buffer, BUFFER_SIZE)&gt;0)
  {
  Serial.println("Something received");
  Serial.println(buffer);
  if(strncmp(buffer, "+IPD,", 5)==0){
    Serial.println("+IPD, found");
    sscanf(buffer+5, "%d,%d", &amp;connectionId, &amp;packet_len);
    if (packet_len &gt; 0) {
    pb = buffer+5;
    while(*pb!=':') pb++;
    pb++;
  
    if (strncmp(pb, "GET /on", 7) == 0) {
    Serial.println("connectionId: " + String(connectionId));
    delay(1000);
    clearESP8266SerialBuffer();
  
    //  sendHTTPResponse(connectionId, strHTML1);
    sendHTTPResponse(connectionId, "{\"message\":true}");
    //sendHTTPResponse(connectionId, strHTML2);
  
    //Close TCP/UDP
    String cmdCIPCLOSE = "AT+CIPCLOSE="; 
    cmdCIPCLOSE += connectionId;
    sendESP8266Cmdln(cmdCIPCLOSE, 1000);
  
    }
    else{
      Serial.println("connectionId: " + String(connectionId));
    delay(1000);
    clearESP8266SerialBuffer();
  
    sendHTTPResponse(connectionId, strHTML1);
    sendHTTPResponse(connectionId, "{\"message\":false}");
    sendHTTPResponse(connectionId, strHTML2);
  
    //Close TCP/UDP
    String cmdCIPCLOSE = "AT+CIPCLOSE="; 
    cmdCIPCLOSE += connectionId;
    sendESP8266Cmdln(cmdCIPCLOSE, 1000);
  
      }
    }
    }
   }
  }
  
  void sendHTTPResponse(int id, String response)
  {
   String cmd = "AT+CIPSEND=";
   cmd += id;
   cmd += ",";
   cmd += response.length();
  
   Serial.println("--- AT+CIPSEND ---");
   sendESP8266Cmdln(cmd, 1000);
  
   Serial.println("--- data ---");
   sendESP8266Data(response, 1000);
   }
  
   boolean waitOKfromESP8266(int timeout)
   {
   do{
   Serial.println("wait OK...");
   delay(1000);
   if(ESP8266.find("OK"))
   {
    return true;
   }
  
   }while((timeout--)&gt;0);
   return false;
   }
  
   boolean cwJoinAP()
   {
   String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
   ESP8266.println(cmd);
   return waitOKfromESP8266(10);
   }
  
  //Send command to ESP8266, assume OK, no error check
  //wait some time and display respond
  void sendESP8266Cmdln(String cmd, int waitTime)
  {
  ESP8266.println(cmd);
  delay(waitTime);
  clearESP8266SerialBuffer();
  }
  
  //Basically same as sendESP8266Cmdln()
  //But call ESP8266.print() instead of call ESP8266.println()
  void sendESP8266Data(String data, int waitTime)
  {
    //ESP8266.print(data);
    ESP8266.print(data);
    delay(waitTime);
    clearESP8266SerialBuffer();
  }
  
  //Clear and display Serial Buffer for ESP8266
  void clearESP8266SerialBuffer()
  {
    Serial.println("= clearESP8266SerialBuffer() =");
    while (ESP8266.available() &gt; 0) {
      char a = ESP8266.read();
      Serial.write(a);
    }
    Serial.println("==============================");
  }
  </code></pre>
  
  <p>And the Android Code is:</p>
  
  <pre><code>            Retrofit retrofit = new Retrofit.Builder()
                      .baseUrl(Constants.protocol+Constants.IP)
                      .addConverterFactory(GsonConverterFactory.create())
                      .build();
              ServerApis service = retrofit.create(ServerApis.class);
              Call&lt;ResponseBody&gt; call = service.left();
              call.enqueue(new Callback&lt;ResponseBody&gt;() {
                  @Override
                  public void onResponse(Response&lt;ResponseBody&gt; response, Retrofit retrofit) {
  
                          Toast.makeText(MainActivity.this,"Left Done.",Toast.LENGTH_SHORT).show();
  
                  }
  
                  @Override
                  public void onFailure(Throwable t) {
                      System.out.print(t.getMessage());
                  }
              });
  </code></pre>
  
  <p>I am getting "Unexpected end of stream" error on receiving the response from the Arduino Mega 2560 server on my Android client. I am using an ESP8266 WiFi module, and I am using Retrofit for the Android. Any hints?</p>