I have a little issue with my code which I am using to read the response from web server.I get the whole information as a data packets so I have to parse the whole response and get the whole packets which server is sending to me. I don’t have problem with the JSON data, but when server sends me a binary data my application crash because I can’t get the whole binary data in 30KB (I’m reading the response in pieces of 30KB). So can anyone just help me a little bit which is the best way to get the response in pieces and parse it?
Here is the sample code which I’m using :
public void handleDataFromSync(String responseBody) {
RPCPacket packet;
X:for(int index=0;index<responseBody.length();index++){
try {
Log.w("Response ","Response size : "+ responseBody.length());
Log.w("Response ","Response : "+ responseBody);
Log.w("","****************Index is : "+index);
Log.i("","BufferString FIRST CHECK OF BUFFER : "+bufferString);
if(bufferString!=null){
shitavaRabota = bufferString.concat(responseBody);
Log.v("","ShitavaRabota : "+shitavaRabota);
//
bufferString = shitavaRabota;
Log.i("","BufferString = SHITAVA RABOTA : "+bufferString);
Log.i("","BufferString SUBSTR : "+bufferString.substring(0));
Log.v("","BufferString LNHTG : "+bufferString.length());
remainingSize+=responseBody.length();
Log.i("","remeinign size : "+remainingSize);
Log.i("","neededSize size : "+neededSize);
if(neededSize<remainingSize){
data=null;
data = bufferString.substring(0, getLastPacketRemainingSize+getSize);
Log.v("","DataSize : "+dataSize);
Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);
Log.i("","bufferString current value : "+bufferString.substring(index, index + dataSize));
index = (index + dataSize)-1;
Log.w("","****************Index is must be : "+index);
Log.e("","Data Size : "+data.length());
first = Base64.decode(data);
String string = new String(first, "UTF-8");
Log.w("Response ","BASE 64 : "+ string);
packet = new RPCPacket( objectIdentificator,
RPCPacketType.getPacketTypeByValue(pType),
RPCOperationType.getByValue(operationType),
objectOId,
id,
dataSize,
hashH,
RPCPacketDataType.getByValue(dataType),
first);
parseRPCPacket(packet);
int bLength = bufferString.length()-dataSize;
responseBody = bufferString.substring(bLength);
Log.e("","ResponseBody = BufferString : "+responseBody);
shitavaRabota = null;
} else if(neededSize>remainingSize) {
bufferString = bufferString.concat(responseBody.substring(index, responseBody.length()-index));
Log.i("","BufferString : "+bufferString);
}
}
Log.v("","index before objectId : "+index);
Log.v("","index before objectId 2 : "+(index+packetFieldSizes[0]));
objectIdentificator = 0;
objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0]));
Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator);
Log.i("","Response current value : "+responseBody.substring(index,index+packetFieldSizes[0]));
index = index+packetFieldSizes[0];
Log.w("","****************Index is : "+index);
pType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[1]));
Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType);
Log.i("","Response current value : "+responseBody.substring(index,index + packetFieldSizes[1]));
index = index + packetFieldSizes[1];
Log.w("","****************Index is : "+index);
operationType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[2]));
Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType);
Log.i("","Response current value : "+responseBody.substring(index,index + packetFieldSizes[2]));
index = index + packetFieldSizes[2];
Log.w("","****************Index is : "+index);
objectOId=null;
objectOId = responseBody.substring(index, index + packetFieldSizes[3]);
Log.w("Response ","UID (CHAR, length 32) : "+ objectOId);
Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[3]));
index = index + packetFieldSizes[3];
Log.w("","****************Index is : "+index);
id=0;
id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4]));
Log.w("Response ","ID (LONGINT) : "+ id);
Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[4]));
index = index + packetFieldSizes[4];
Log.w("","****************Index is (must be 102) : "+index);
dataSize = 0;
dataSize = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[5]));
Log.w("Response ","Data Size (LONGINT) : "+ dataSize);
Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[5]));
index = index + packetFieldSizes[5];
Log.w("","****************Index is (must be 134) : "+index);
hashH=null;
hashH = responseBody.substring(index,index + packetFieldSizes[6]);
Log.w("Response ","Data Hash (CHAR, length 32 : "+ hashH);
Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[6]));
index = index + packetFieldSizes[6];
Log.w("","****************Index is (must be 166) : "+index);
dataType= 0;
dataType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[7]));
Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType);
Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[7]));
index = index + packetFieldSizes[7];
Log.w("","****************Index is (must be 169) : "+index);
neededSize = dataSize; // cherna magiq
remainingSize = (responseBody.length()-index);
Log.v("","NeededSize : "+neededSize);
Log.v("","Response length "+remainingSize);
getSize = neededSize - remainingSize;
Log.v("","getSize : "+getSize);
if(neededSize<remainingSize){
data=null;
data = responseBody.substring(index, index + dataSize);
Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);
Log.i("","Response current value : "+responseBody.substring(index, index + dataSize));
index = (index + dataSize)-1;
Log.w("","****************Index is must be : "+index);
first = Base64.decode(data);
String string = new String(first, "UTF-8");
Log.w("Response ","BASE 64 : "+ string);
packet = new RPCPacket( objectIdentificator,
RPCPacketType.getPacketTypeByValue(pType),
RPCOperationType.getByValue(operationType),
objectOId,
id,
dataSize,
hashH,
RPCPacketDataType.getByValue(dataType),
first);
parseRPCPacket(packet);
} else if(neededSize>remainingSize){
Log.i("","Sorry you don't have enough space, take the second part of stream.");
getLastPacketRemainingSize = remainingSize;
if(bufferString==null){
bufferString = responseBody.substring(index, remainingSize);
Log.i("","BufferString IF : "+bufferString);
} else if(bufferString!=null){
bufferString = bufferString.concat(responseBody.substring(index, remainingSize));
Log.i("","BufferString ELSE : "+bufferString);
}
break X;
}
}catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}/* catch (StringIndexOutOfBoundsException e){
e.printStackTrace();
}*/
}
}
Your code snippet is rather hard to read. What’s
RPCPacket? What’sbufferString? AreLoglines really essential to understand the logic?Do you really have to repeat the main loop once per every character of
responseBody, which is what you’re doing now?Also, the whole notion of having response as
Stringkinda smells unfriendly to binary data.EDIT: to get the first 32 bytes from a byte array, use:
And please don’t treat binary data as String. Corruption will be almost inevitable.