Solution:
I had solved the problem. The brief description for the solution is below :
Retrofit Response -> InputStream ->java bytes[] -> treat byte as short or int -> int[] or short[].
Code
Below is my detail demo code.
Android App client, which receiver 8-bit unsigned integer array.
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);
}
RetrofitDemoActivity.java
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 is as demo server,which reponse 8-bit unsigned integer array.
server.js
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/');
Ref:
- com.squareup.retrofit:retrofit:1.9.0
- io.reactivex:rxjava:1.1.0
- io.reactivex:rxandroid:1.1.0