Created
December 1, 2014 07:00
-
-
Save eygraber/5b150d549196168fa2e8 to your computer and use it in GitHub Desktop.
Response DataCallback Never Called #287 - Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pacakge; | |
import android.net.Uri; | |
import com.koushikdutta.async.DataEmitter; | |
import com.koushikdutta.async.DataSink; | |
import com.koushikdutta.async.Util; | |
import com.koushikdutta.async.callback.CompletedCallback; | |
import com.koushikdutta.async.http.AsyncHttpClient; | |
import com.koushikdutta.async.http.AsyncHttpRequest; | |
import com.koushikdutta.async.http.AsyncHttpResponse; | |
import com.koushikdutta.async.http.Multimap; | |
import com.koushikdutta.async.http.body.AsyncHttpRequestBody; | |
import com.koushikdutta.async.http.callback.HttpConnectCallback; | |
import retrofit.client.Client; | |
import retrofit.client.Header; | |
import retrofit.client.Request; | |
import retrofit.client.Response; | |
import retrofit.mime.TypedInput; | |
import retrofit.mime.TypedOutput; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ExecutionException; | |
public class AndroidAsyncClient implements Client { | |
private static HttpConnectCallback ignoreCallback = new HttpConnectCallback() { | |
@Override | |
public void onConnectCompleted(Exception e, AsyncHttpResponse asyncHttpResponse) { | |
//do nothing | |
} | |
}; | |
private final AsyncHttpClient client = AsyncHttpClient.getDefaultInstance(); | |
public AndroidAsyncClient() {} | |
@Override | |
public Response execute(Request request) throws IOException { | |
AsyncRequest req = new AsyncRequest(request); | |
AsyncHttpResponse response; | |
try { | |
response = client.execute(req, ignoreCallback).get(); | |
} catch (InterruptedException|ExecutionException e) { | |
throw new IOException("Request interrupted"); | |
} | |
return wrapResponse(response); | |
} | |
private Response wrapResponse(AsyncHttpResponse response) { | |
TypedInputStream responseBody = new TypedInputStream(response); | |
int status = response.code(); | |
String message = response.message(); | |
if(message == null) { | |
message = ""; | |
} | |
List<Header> headers = new ArrayList<>(); | |
Multimap headerMap = response.headers().getMultiMap(); | |
for(String key : headerMap.keySet()) { | |
for(String header : headerMap.get(key)) { | |
headers.add(new Header(key, header)); | |
} | |
} | |
return new Response(response.getRequest().getUri().toString(), status, message, headers, responseBody); | |
} | |
private static class AsyncRequest extends AsyncHttpRequest { | |
public AsyncRequest(Request request) { | |
super(Uri.parse(request.getUrl()), request.getMethod()); | |
for(Header header : request.getHeaders()) { | |
addHeader(header.getName(), header.getValue()); | |
} | |
retrofit.mime.TypedOutput body = request.getBody(); | |
if(body != null) { | |
setBody(new TypedOutputBody(body)); | |
} | |
} | |
} | |
private static class TypedOutputBody implements AsyncHttpRequestBody<TypedOutput> { | |
private TypedOutput output; | |
public TypedOutputBody(TypedOutput output) { | |
this.output = output; | |
} | |
@Override | |
public void write(AsyncHttpRequest request, DataSink sink, CompletedCallback completed) { | |
ByteArrayOutputStream os = new ByteArrayOutputStream((int) output.length()); | |
try { | |
output.writeTo(os); | |
} catch (IOException e) { | |
throw new RuntimeException("Could not use body", e); | |
} | |
byte[] bodyBytes = os.toByteArray(); | |
InputStream is = new ByteArrayInputStream(bodyBytes); | |
Util.pump(is, Integer.MAX_VALUE, sink, completed); | |
} | |
@Override | |
public void parse(DataEmitter emitter, CompletedCallback completed) { | |
completed.onCompleted(null); | |
} | |
@Override | |
public String getContentType() { | |
return output.mimeType(); | |
} | |
@Override | |
public boolean readFullyOnRequest() { | |
return false; | |
} | |
@Override | |
public int length() { | |
return -1; | |
} | |
@Override | |
public TypedOutput get() { | |
return output; | |
} | |
} | |
private static class TypedInputStream implements TypedInput { | |
private AsyncHttpResponse response; | |
public TypedInputStream(AsyncHttpResponse response) { | |
this.response = response; | |
} | |
@Override | |
public String mimeType() { | |
return response.headers().get("Content-Type"); | |
} | |
@Override | |
public long length() { | |
return Long.parseLong(response.headers().get("Content-Length")); | |
} | |
@Override | |
public InputStream in() throws IOException { | |
try { | |
return new InputStreamParser().parse(response).get(); | |
} catch (InterruptedException|ExecutionException e) { | |
throw new IOException("There was an error parsing the request", e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment