Android阅读&解析HttpUrlConnection响应

时间:2011-10-23 15:05:16

标签: android response httpurlconnection

我的代码有点问题,我用它来读取来自Web服务器的响应。我将整个信息作为数据包获取,因此我必须解析整个响应并获取服务器发送到的整个数据包我。我没有JSON数据的问题,但是当服务器向我发送二进制数据时,我的应用程序崩溃,因为我无法获得30KB的整个二进制数据(我正在读取30KB的响应)。那么,任何人都可以帮助我一点点,这是获得分片响应并解析它的最佳方法吗? 以下是我正在使用的示例代码:

    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();
    }*/
}
}

1 个答案:

答案 0 :(得分:1)

您的代码段难以阅读。什么是RPCPacket?什么是bufferStringLog行是否对理解逻辑至关重要?

你是否真的必须为responseBody的每个角色重复一次主循环,这就是你现在正在做的事情?

此外,响应为String的整个概念有点对二进制数据不友好。

编辑:要从字节数组中获取前32个字节,请使用:

byte []Source;
byte []First = Arrays.copyOf(Source, 32);

请不要将二进制数据视为String。腐败几乎是不可避免的。