public interface IDataService {
// Request URL: https://demo.testdata.com/getData/order?timeStamp=1491986181670&callerId=android_platform
// Request Method: GET
// Response Content-Type: application/octet-stream
@GET("https://demo.testdata.com/getData/" + "{type}")
Observable<Response> getTestData(@Path(value = "type", encode = false) String type
, @Query("callerId") String callerId, @Query("timeStamp") long timeStamp);
}
在TestActivity.java中
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();
}
答案 0 :(得分:1)
将解决方案从问题转移到答案:
解决方案:
我解决了这个问题。解决方案的简要说明如下:
改造响应 - &gt; InputStream - &gt; java bytes [] - &gt;将字节视为short或int - &gt; int []或short []。
代码
以下是我的详细演示代码。
Android App客户端,其接收器为8位无符号整数数组。
IRetrofitService.javaRetrofitDemoActivity.javapublic 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 { } } }); } }
Node.JS作为演示服务器,响应8位无符号整数数组。
server.jsvar 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/');
价:
- com.squareup.retrofit:改型:1.9.0
- io.reactivex:rxjava:1.1.0
- io.reactivex:rxandroid:1.1.0