Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload Image and String to Server - Secret Key Not Match in Response Error

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.21k
    Comment on it
    Hi can anyone help me with this. i have to pass secret key . the method which i did is correct? am getting secret key not match in response .i also attached postman screen shot please see this.
    
    
    
    private class uploadFileToServerTask extends AsyncTask <String, String, Object> {
        DataOutputStream outputStream = null;
        @Override
        protected String doInBackground(String... args) {
            try {
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                int bytesRead, bytesAvailable, bufferSize;
                byte[] buffer;
                @SuppressWarnings("PointlessArithmeticExpression")
                int maxBufferSize = 1 * 1024 * 1024;
                String result = "";
                String file = args[0];
    
                java.net.URL url = new URL( ApplicationConstant.BASE_URL );
                Log.d( ApplicationConstant.TAG, "url " + url );
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
          // Allow Inputs &amp; Outputs.
                connection.setDoInput( true );
                connection.setDoOutput( true );
                connection.setUseCaches( false );
    
                // Set HTTP method to POST.
                connection.setRequestMethod( "POST" );
                connection.setRequestProperty( "Connection", "Keep-Alive" );
                connection.setRequestProperty("Accept", "application/xml");
                connection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary );
                //connection.setRequestProperty( "secret_key", secret_key);
                FileInputStream fileInputStream;
                {
                    try {
                        outputStream= new DataOutputStream( connection.getOutputStream() );
                        outputStream.writeBytes( twoHyphens + boundary + lineEnd );
                        outputStream.writeBytes( "Content-Disposition: form-data; name=\"file\";file=\"" + file + "\"" + lineEnd );
                        outputStream.writeBytes( lineEnd );
                        outputStream.writeBytes( twoHyphens + boundary + lineEnd );
                        outputStream.writeBytes( "Content-Disposition: form-data; name=\"secret_key\";Value=\"" +secret_key + "\"" + lineEnd );
                        //outputStream.writeBytes( query );
                        Log.d( ApplicationConstant.TAG, "file " + file );
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
    
                    fileInputStream = new FileInputStream( file );
    
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min( bytesAvailable, maxBufferSize );
    
                    buffer = new byte[bufferSize];
                    // Read file
                    // bytesRead = fileInputStream.read( buffer, 0, bufferSize );
                    outputStream.write( buffer, 0, bufferSize );
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min( bytesAvailable, maxBufferSize );
                    bytesRead = fileInputStream.read( buffer, 0, bufferSize );
    
                    outputStream.writeBytes( lineEnd );
                    outputStream.writeBytes( twoHyphens + boundary + twoHyphens + lineEnd );
                }
                int serverResponseCode = connection.getResponseCode();
                String serverResponseMessage = connection.getResponseMessage();
                Log.d( "serverResponseCode", "" + serverResponseCode );
                Log.d( "serverResponseMessage", "" + serverResponseMessage );
    
                //Response from server
                String response = "";
                if (serverResponseCode == HttpURLConnection.HTTP_OK) {
                    InputStream responseStream = new
                            BufferedInputStream( connection.getInputStream());
    
                    BufferedReader responseStreamReader =
                            new BufferedReader( new InputStreamReader( responseStream ));
    
                    String line = "";
                    StringBuilder stringBuilder = new StringBuilder();
    
                    while ((line = responseStreamReader.readLine()) != null) {
                        stringBuilder.append( line ).append( "\n" );
                    }
                    responseStreamReader.close();
    
                    response = stringBuilder.toString();
                    connection.disconnect();
                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
    
                    if (serverResponseCode == 200) {
    
                        return "true";
                    }
                } else {
                    throw new IOException( "Server returned non-OK status: " + serverResponseCode );
                }
    
                return response;
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (ProtocolException e1) {
                e1.printStackTrace();
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }catch(
                    Exception e)
    
            {
                e.printStackTrace();
            }
            return"false";
        }
    

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: