After service call automatically variable value changing in loop

只愿长相守 提交于 2020-01-06 05:56:30

问题


I uploading zip file using chunk method. After some iteration completed in while loop TotalParts variable automatically changed to some value. When connected to debugger its works fine for some time, but when disconnected again value changed for some files. But, after some iteration finally it will upload.Value is changing in Jsonbody in http service.

Actually TotalParts variable is final and also not declared inside while loop, although its changing to some value.

public Boolean uploadFile(String filePath,int chunkSize,JSONObject jsonObj)
{
    boolean dataUploaded=false;
    InputStream inStream=null;
    try
    {
        File zipFile = new File(filePath);

        int fileSize =  (int)zipFile.length();
        inStream = new BufferedInputStream( new FileInputStream(filePath));

       final  int totalparts =(int) Math.ceil((double)fileSize / (double)chunkSize);
        Logger.i("outside loop- file size", String.valueOf(fileSize));
        Logger.i("outside loop- chunk Size", String.valueOf(chunkSize));
        Logger.i("outside loop- file totalparts", String.valueOf(totalparts));
        System.out.println(totalparts);

        byte [] data = new byte[chunkSize];
        int index=0,checkCount=0;
        boolean readNextData=true;

        while ( true )
        {
            if(readNextData) {
                inStream.read(data, 0, chunkSize);
                checkCount=0;
                ++index;
            }
            if(totalparts > 200){
                break;
            }

            Logger.i("before upload chunkList index", String.valueOf(index));
            Logger.i("before upload file totalparts", String.valueOf(totalparts));
            System.out.println(totalparts);

            jsonObj.put("fileName", zipFile.getName());
            jsonObj.put("base64Content", convertToBase64EncodingFromByteArray(data));
            jsonObj.put("partNo", index);
            jsonObj.put("totalParts", totalparts);
           //jsonObj.put("md5checksum","A0A0");
            jsonObj.put("md5checksum",CheckSum.getMd5OfByteArray(data));

            String responseData = sendData(jsonObj.toString());

            Logger.v("responseData",responseData);
            JSONObject responData=new JSONObject(responseData);
            String errorCode=(String) responData.get("ErrorCode");

            if(!errorCode.equals("0"))
            {
                checkCount++;
                readNextData=false;
                if(checkCount>4)
                    break;

            } else  if(errorCode.equals("0"))
            {
                readNextData=true;
                if( index==totalparts ){
                    dataUploaded=true;
                    break;
                }
            }else
            {
                break;
            }

            // status = fileUpload(chunkList.get(i), "videoChunk" + "/" + chunkList.get(i).split("/")[4]);
            Logger.i("after upload chunkList index status",String.valueOf(index) );
            Logger.i("after upload file totalparts", String.valueOf(totalparts));
            System.out.println(totalparts);


        }


    }catch(Exception e){
        Logger.ex(e);

    }finally {
        if(inStream!=null)

        {
            try {
                inStream.close();
            }catch (Exception e)
            {
                Logger.ex(e);
            }
        }
    }

    return dataUploaded;
}

Please check below log files about value of TotalParts-

    2019-07-26 11:27:25.463 3003-3003/? D/SamsungAlarmManager: setInexact Listener (T:2/F:0/AC:false) 20190726T112818 - CU:1000/CP:3003
2019-07-26 11:27:25.474 7139-7361/Package Name  I/System.out: outside loop- file size - 5943628
2019-07-26 11:27:25.475 7139-7361/Package Name  I/System.out: outside loop- chunk Size - 500000
2019-07-26 11:27:25.475 7139-7361/Package Name  I/System.out: outside loop- file totalparts - 12
2019-07-26 11:27:25.478 7139-7361/Package Name  I/System.out: before upload chunkList index - 1
2019-07-26 11:27:25.478 7139-7361/Package Name  I/System.out: before upload file totalparts - 12
2019-07-26 11:27:25.523 7139-7144/Package Name  I/zygote: Do partial code cache collection, code=500KB, data=365KB
2019-07-26 11:27:25.523 7139-7144/Package Name  I/zygote: After code cache collection, code=500KB, data=365KB
2019-07-26 11:27:25.523 7139-7144/Package Name  I/zygote: Increasing code cache capacity to 2MB
2019-07-26 11:27:25.652 7139-7361/Package Name  E/Service Started: Service Started
2019-07-26 11:27:25.661 3003-7178/? D/SSRM:X: SIOP:: AP:332(344,0) BAT:273(273,0) CHG:0(0,0) ATC:0(0,0) 
2019-07-26 11:27:25.671 3003-10699/? D/ConnectivityService: filterNetworkStateForUid() uid: 10012 networkInfo: [type: WIFI[] - WIFI, state: CONNECTED/CONNECTED, reason: (unspecified), extra: "ISSLT 1037", failover: false, available: true, roaming: false]
2019-07-26 11:27:26.253 7139-7361/Package Name  I/System.out: jsonbody before response totalparts-12
2019-07-26 11:27:26.258 7139-7361/Package Name  I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2019-07-26 11:27:26.258 7139-7361/Package Name  I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2019-07-26 11:27:26.326 32122-7848/? I/a: {SN disabled}
2019-07-26 11:27:26.866 3003-9739/? D/ConnectivityService: filterNetworkStateForUid() uid: 10117 networkInfo: [type: WIFI[] - WIFI, state: CONNECTED/CONNECTED, reason: (unspecified), extra: "ISSLT 1037", failover: false, available: true, roaming: false]
2019-07-26 11:27:26.873 7755-7755/? D/ConnectivityManager: requestNetwork; CallingUid : 10117, CallingPid : 7755
2019-07-26 11:27:26.877 3003-4049/? D/ConnectivityService: listenForNetwork for uid/pid:10117/7755 NetworkRequest [ LISTEN id=161, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&FOREGROUND] ]
2019-07-26 11:27:26.879 3003-3242/? D/ConnectivityService: rematching NetworkAgentInfo [WIFI () - 605]
2019-07-26 11:27:27.397 7139-7361/Package Name  I/System.out: jsonbody after response totalparts-319553632
2019-07-26 11:27:27.398 7139-7361/Package Name  V/responseData: {"ErrorCode":"0","ErrorMessage":"","Result":"True","DeviceIMEI":null,"UserId":null,"UserName":null,"IRNumber":null,"UpdatedDate":"0001-01-01T00:00:00","TokenID":null}
2019-07-26 11:27:27.402 7139-7361/Package Name  I/System.out: after upload chunkList index status -1
2019-07-26 11:27:27.402 7139-7361/Package Name  I/System.out: after upload file totalparts -319553632

when calling service only its changing value in jsonbody-

         JSONObject object = new JSONObject(jsonbody);
         //here its value changed  
            System.out.println("jsonbody before response totalparts-"+object.getString("totalParts"));
            //normal http connection
            HttpURLConnection conn = null;
            // 2. open the HTTP connection
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);
            // 3.2 Sets the flag indicating this connection does not use cache
            conn.setUseCaches(false);
            // 3.3 Sets the length of the body
            conn.setDoInput(true);
            // 3.4 Sets the method to POST
            conn.setRequestMethod("POST");
            // 3.5 Sets Content-Type
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            OutputStream outputStream = conn.getOutputStream();

            try {


                try {
                    outputStream.write(jsonbody.toString().getBytes("UTF-8"));
                    outputStream.close();
                } catch (Exception e) {
                    Logger.ex(e);
                    EvidensFramework.mErrorLogger.writeLog(null, this.getClass().getSimpleName()
                            + ": getResponse", e.getStackTrace().toString(), "Error");
                    return "";
                }

                // handle the response
                int status = conn.getResponseCode();
                if (status != 200) {
                    InputStream is = conn.getErrorStream();
                    Logger.d("", "");
                    throw new IOException("Connection failed with error code " + status);
                }
                // Get Response
                InputStream is = conn.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer response = new StringBuffer();
                while ((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\n');
                }
                System.out.println("jsonbody after response totalparts-"+object.getString("totalParts"));
                resp = response.toString();
                rd.close(); 

plz give any suggestion or solution for this

来源:https://stackoverflow.com/questions/57175610/after-service-call-automatically-variable-value-changing-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!