[英]Java Android AsyncHttpClient convert byte[]responseBody to InputStream
I want to create a methos which return a InputStream I did this : 我想创建一个返回InputStream的方法,我这样做是:
public static InputStream sendGetDataRequest(Context context,
String version, String power, String lon, String lat, String radius) throws MalformedURLException, ConnectTimeoutException, IOException
{
AsyncHttpClient client = new AsyncHttpClient();
String url = Util.getServerUrl(context) + "/GetData";
// ContentValues values=new ContentValues();
RequestParams values = new RequestParams();
values.put("token", String.valueOf(E_Gps.TOKEN));
values.put("xml", login_xml);
StringEntity entity = null;
try {
entity = new StringEntity(values.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
entity.setContentType(new BasicHeader("Content-Type","application/xml"));
final InputStream[] is = new InputStream[1];
final InputStream inputStream;
client.post(context,url,values , new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.e("success_noti", new String(responseBody) + "");
is[0] = new ByteArrayInputStream(responseBody);
Log.e("Status " , statusCode + " ");
Log.e("is" , convertStreamToString(is[0]));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e("success_noti", new String(responseBody) + "");
Log.e("Status " , statusCode + " ");
}
});
return is[0];
}
And I have probleme when I return is[0] is a null but when I log in onSuccess : 当我返回is [0]为空但登录onSuccess时遇到问题:
Log.e("is" , convertStreamToString(is[0]));
is not null why I have null object is[0] ? 不是null为什么我有null对象is [0]?
Because client.post
is an asynchronous call while return is[0]
is a synchronous. 因为
client.post
是异步调用,而return is[0]
是同步的。 This means that is[0]
is null as long as the async call is not done yet. 这意味着只要异步调用尚未完成,
is[0]
为null。 One way to solve is by making sendGetDataRequest
return void
and instead accepts a callback eg 一种解决方法是使
sendGetDataRequest
返回void
,而接受回调,例如
public static void sendGetDataRequest(final YourCallback callback, ...)
Create a new Interface named YourCallback
创建一个名为
YourCallback
的新接口
public interface YourCallback{
void onSuccess(String string);
void failure();
}
And use that Callback inside the async method 并在异步方法中使用该回调
client.post(context,url,values , new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(convertStreamToString(is[0]));
}
@Override
public void onFailure(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. 顺便说一句,您还可以使用android包中的
AsyncTask
来完成上述所有操作。 It's personal preference. 这是个人喜好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.