当内容类型=应用程序/八位字节流时,Android如何处理改装响应? 改装请求界面: 使用改装请求界面:
在TestActivity.java中当内容类型=应用程序/八位字节流时,Android如何处理改装响应? 改装请求界面: 使用改装请求界面:,android,retrofit,Android,Retrofit,在TestActivity.java中 public Observable<String> getTestData(IDataService dataService) { final BehaviorSubject<String> subject = BehaviorSubject.create(); if (null == dataService) { subject.onError(new NullPointer
public Observable<String> getTestData(IDataService dataService) {
final BehaviorSubject<String> subject = BehaviorSubject.create();
if (null == dataService) {
subject.onError(new NullPointerException());
return subject;
}
bindObservable(dataService.getTestData("order", "android_platform", new Date().getTime())).subscribeOn(Schedulers.io())
.subscribe(new Subscriber<Response>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
subject.onError(e);
}
@Override
public void onNext(retrofit.client.Response response) {
// TODO: how to handle the response body content?
// In general, response is json string. Then parse and convert json string to Object.
// Now Response Content-Type = application/octet-stream, How to handle it?
}
});
return subject.asObservable();
}
问题:一般来说,响应内容类型=application/json,将json字符串解析为Object。现在,响应内容类型=应用程序/八位字节流,如何处理?
裁判:
改装:改装:1.9.0
io.reactivex:rxjava:1.1.0
io.reactivex:rxandroid:1.1.0
将解决方案从问题移至答案: 解决方案: 我已经解决了这个问题。解决方案的简要说明如下:
改装响应->输入流->java字节[]->将字节视为短或int->int[]或短[] 密码 下面是我的详细演示代码 Android应用程序客户端,接收8位无符号整数数组。 IRetrofitService.java
public class IRetrofitService {
// Local Node.JS Server
/**
* FAQ: Retrofit: java.net.ConnectException: Connection refused
* Reason:Node.js server PI is http://127.0.0.1:8888/.
* If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8888/ .
Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address
Ref: https://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused
*/
private static final String PATH = "http://10.0.2.2:8888";
public interface ApiManagerService {
@GET("/")
@Headers("Content-Type:application/octet-stream")
Observable<Response> getUnsignedBytes();
}
private static final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(PATH).setLogLevel(RestAdapter.LogLevel.FULL).build();
public static final ApiManagerService apiManager = restAdapter.create(ApiManagerService.class);
}
裁判:
改装:改装:1.9.0
io.reactivex:rxjava:1.1.0
io.reactivex:rxandroid:1.1.0
粘贴您的响应请删除响应->输入流->java字节[]->将字节视为短或int->int[]或短[]。
public class IRetrofitService {
// Local Node.JS Server
/**
* FAQ: Retrofit: java.net.ConnectException: Connection refused
* Reason:Node.js server PI is http://127.0.0.1:8888/.
* If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8888/ .
Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address
Ref: https://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused
*/
private static final String PATH = "http://10.0.2.2:8888";
public interface ApiManagerService {
@GET("/")
@Headers("Content-Type:application/octet-stream")
Observable<Response> getUnsignedBytes();
}
private static final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(PATH).setLogLevel(RestAdapter.LogLevel.FULL).build();
public static final ApiManagerService apiManager = restAdapter.create(ApiManagerService.class);
}
import android.util.Log;
import android.widget.Toast;
import com.hades.android.example.R;
import com.hades.android.example.framework.BaseActivity;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import butterknife.OnClick;
import retrofit.client.Response;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* RxJava and RxAndroid , Retrofit to request data from server.
* Need <uses-permission android:name="android.permission.INTERNET" />
*/
public class RetrofitDemoActivity extends BaseActivity {
public static final String TAG = RetrofitDemoActivity.class.getSimpleName();
private int[] singedBytes;
// TODO: IRetrofitService.apiManager.getUnsignedBytes()
private Observable<Response> getUnsignedBytes() {
return IRetrofitService.apiManager.getUnsignedBytes();
}
@OnClick(R.id.requestUnsignedBytes)
public void requestUnsignedBytes() {
Log.d(TAG, "requestUnsignedBytes,----->");
bindObservable(getUnsignedBytes()).observeOn(Schedulers.io()).subscribe(new Subscriber<Response>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
/**
* Response content is 8 bites unsigned byte
* [246, 254, 255, 0, 10, 126, 127, 128, 129, 253, 254, 255, 0, 1, 232]
*/
/**
s,buf_size=15,read_buf_size=15
start parse
origin = -10, afterTransValue = 246
origin = -2, afterTransValue = 254
origin = -1, afterTransValue = 255
origin = 0, afterTransValue = 0
origin = 10, afterTransValue = 10
origin = 126, afterTransValue = 126
origin = 127, afterTransValue = 127
origin = -128, afterTransValue = 128
origin = -127, afterTransValue = 129
origin = -3, afterTransValue = 253
origin = -2, afterTransValue = 254
origin = -1, afterTransValue = 255
origin = 0, afterTransValue = 0
origin = 1, afterTransValue = 1
origin = -24, afterTransValue = 232
end parse
*/
@Override
public void onNext(Response response) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = response.getBody().in();
bufferedInputStream = new BufferedInputStream(inputStream);
int buf_size = inputStream.available();
byte[] bytes = new byte[buf_size];
int read_buf_size = bufferedInputStream.read(bytes, 0, buf_size);
Log.d(TAG, "requestUnsignedBytes,buf_size=" + buf_size + ",read_buf_size=" + read_buf_size);
if (null == bytes) {
return;
}
if (null != singedBytes) {
singedBytes = null;
}
singedBytes = new int[buf_size];
Log.d(TAG, "requestUnsignedBytes, start parse");
for (int i = 0; i < bytes.length; i++) {
/**
* First, read as the 8 bites signed byte.
* Second, get the signed tag as the value.
*/
byte origin = bytes[i];
int afterTransValue = 0xFF & origin;
Log.d(TAG, "requestUnsignedBytes,origin = " + bytes[i] + ", afterTransValue = " + afterTransValue);
singedBytes[i] = afterTransValue;
}
Log.d(TAG, "requestUnsignedBytes, end parse");
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
});
}
}
var http = require('http');
var server = http.createServer(function (request, response) {
var sampleBytes = new Uint8Array(15);
sampleBytes[0] = -10;
sampleBytes[1] = -2;
sampleBytes[2] = -1;
sampleBytes[3] = 0;
sampleBytes[4] = 10;
sampleBytes[5] = 126;
sampleBytes[6] = 127;
sampleBytes[7] = 128;
sampleBytes[8] = 129;
sampleBytes[9] = 253;
sampleBytes[10] = 254;
sampleBytes[11] = 255;
sampleBytes[12] = 256;
sampleBytes[13] = 257;
sampleBytes[14] = 1000;
console.log(sampleBytes);
var buffer = new Buffer(sampleBytes);
response.writeHead(200,{'Access-Control-Allow-Origin':'*','Access-Control-Allow-Method':'GET,POST','Content-Type':'application/octet-stream'});
response.write(buffer);
response.end();
});
server.listen(8888,"localhost",function(){
console.log("start monitor...");
});
console.log('Server running at http://127.0.0.1:8888/');