http-request
http-request copied to clipboard
APP crash bug:call body() method 2 times will result in app crash
just like this,and app will crash
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity);
new Thread() {
@Override
public void run(){
HttpRequest response = HttpRequest.get("http://www.google.com");
String t1=response.body();
String t2=response.body(); //call body() 2 times
}
}.start();
}
Yes, this is happening to me also, I had to store the body() result in a var to be able to access it multiple times. Had me looking for the problem some hours. A fix for this would be great.
@ueinohakono @kaoz70 can you provide the stack trace you are seeing? Thanks.
The body() method will open the stream to read all content each time. Back to the code, the 1st call will close the input stream associated with the connection. So, the issue came out when you do the 2nd call. I think this can be avoid by handle by in the code like cache the response body.
The stack trace:
Exception in thread "main" com.github.kevinsawicki.http.HttpRequest$HttpRequestException: java.io.IOException: stream is closed at com.github.kevinsawicki.http.HttpRequest$Operation.call(HttpRequest.java:689) at com.github.kevinsawicki.http.HttpRequest.copy(HttpRequest.java:2573) at com.github.kevinsawicki.http.HttpRequest.body(HttpRequest.java:1724) at com.github.kevinsawicki.http.HttpRequest.body(HttpRequest.java:1739) at com.foxty.http.HttpRequestTest.main(HttpRequestTest.java:11) Caused by: java.io.IOException: stream is closed at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3308) at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3333) at java.io.BufferedInputStream.read1(BufferedInputStream.java:284) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.FilterInputStream.read(FilterInputStream.java:107) at com.github.kevinsawicki.http.HttpRequest$8.run(HttpRequest.java:2579) at com.github.kevinsawicki.http.HttpRequest$8.run(HttpRequest.java:2573) at com.github.kevinsawicki.http.HttpRequest$Operation.call(HttpRequest.java:683) ... 4 more