Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Upload Image and JSON to Server

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 834
    Answer it

    Hi am Fresher in android . am creating a app which i need to upload captured image and JSON data which i got from QRcode to PHP server. But am getting Secret_key not matched message when i debug. 

    I Checked API from postman the server is responding with the file path which i uploaded. but from code am getting secret key not match. please help guys.

    this is my code

    private class uploadFileToServerTask extends AsyncTask <String, String, Object> {
       private DataOutputStream outputStream;;
        @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 filename = 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( "Content-Type", "multipart/form-data;boundary=" + boundary );
                connection.setRequestProperty( "secret_key", ApplicationConstant.secret_key);
            
                outputStream= new DataOutputStream( connection.getOutputStream() );
    
                FileInputStream fileInputStream;
    
                {
                    outputStream.writeBytes( twoHyphens + boundary + lineEnd );
                    outputStream.writeBytes( "Content-Disposition: form-data; name=\"file\";filename=\"" + filename + "\"" + lineEnd );
                    outputStream.writeBytes( lineEnd );
                    outputStream.writeBytes( twoHyphens + boundary + lineEnd );
                    //outputStream.writeBytes( query );
                    Log.d( ApplicationConstant.TAG, "filename " + filename );
    
    
                    fileInputStream = new FileInputStream( filename );
    
                    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 Answer(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: