У меня есть небольшая проблема с моим кодом, который я использую для чтения ответа от веб-сервера. Я получаю всю информацию в виде пакетов данных, поэтому мне нужно проанализировать весь ответ и получить все пакеты, на которые сервер отправляетмне.У меня нет проблем с данными JSON, но когда сервер отправляет мне двоичные данные, происходит сбой моего приложения, потому что я не могу получить все двоичные данные в 30 КБ (я читаю ответ частями по 30 КБ).Так может кто-нибудь просто помочь мне немного, что является лучшим способом получить ответ по частям и разобрать его?Вот пример кода, который я использую:
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();
}*/
}
}