Java Android Asynchttpclient Convert Byte[]responsebody To Inputstream
I want to create a methos which return a InputStream I did this : public static InputStream sendGetDataRequest(Context context, St
Solution 1:
Because client.post
is an asynchronous call while return is[0]
is a synchronous. This means that is[0]
is null as long as the async call is not done yet. One way to solve is by making sendGetDataRequest
return void
and instead accepts a callback e.g.
publicstaticvoidsendGetDataRequest(final YourCallback callback, ...)
Create a new Interface named YourCallback
publicinterfaceYourCallback{
voidonSuccess(String string);
voidfailure();
}
And use that Callback inside the async method
client.post(context,url,values , newAsyncHttpResponseHandler(Looper.getMainLooper()) {
@OverridepublicvoidonSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(convertStreamToString(is[0]));
}
@OverridepublicvoidonFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.onFailure();
}
}
Finally you call your static method like this
sendGetDataRequest(new YourCallback(){/* Override the methods */}, ...);
By the way you could also use AsyncTask
from the android package which does everything above. It's personal preference.
Post a Comment for "Java Android Asynchttpclient Convert Byte[]responsebody To Inputstream"