Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:47:28+00:00 2026-06-17T20:47:28+00:00

I need to send a file and some data to PHP server.I am using

  • 0

I need to send a file and some data to PHP server.I am using a thread for that and a service that executes every 15 min.It is working fine when network is available.But when not in network it stops sending the file (if the size of file is relatively bigger).Upon applying logs I got to see that ,the respnse from the server is not fetched.And it got struk.Moreover it is sending Unknownhost exception when trying to send the file for first time.What could be the problem?please help.

Here is the code:-

public void run(){


    while (keepRunning){
        isRunning = true;

        SystemClock.sleep(900000);



        File tempFile = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.wherephone.helloandroid/log_files/", "barcode_log");
        File tempFile1 = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.wherephone.helloandroid/log_files/", "data_log.txt");


        if(tempFile.exists())
        {           

        Log.d(TAG," in keep running");  

        String[] files =  (new File(Environment.getExternalStorageDirectory() + PATH)).list();

        Log.d(TAG," below file list");  

        SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Date curDate = new Date();
        String s= "data_log";//df3.format(curDate);


        Log.d("log",files[0]);


        File file = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.wherephone.helloandroid/log_files/","barcode_log");

        String new_file_name =s+".txt";
        // File (or directory) with new name
        File file2 = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.wherephone.helloandroid/log_files/",new_file_name);
        // Rename file (or directory)
        if(!tempFile1.exists()){

            boolean success = file.renameTo(file2);
            Log.d(TAG," file name created");


            if (success) {
                // File was not successfully renamed

                Log.d(TAG," file rename success");
            }
            else
            {
                Log.d(TAG," file not rename success");
            }
        } 

        ts.writeBarcodeToFile(" S L", "");
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = Environment.getExternalStorageDirectory() + "/Android/data/com.wherephone.helloandroid/log_files/"+new_file_name;//zipName;
        String urlServer = "http://xyz.com/xyz.php?fname="+s+".txt";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "V2ymHFg03ehbqgZCaKO6jy";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 2*1024*1024;

        FileInputStream fileInputStream=null;

        try
        {


        ts.writeBarcodeToFile(" in try sl", "");
        fileInputStream = new FileInputStream(new File(pathToOurFile) );

        InetAddress iAddr = InetAddress.getByName("http://xyz.com");
        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        connection.setConnectTimeout(20000);

        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);




        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        ts.writeBarcodeToFile("before while sl ", "");
        while (bytesRead > 0)
        {
        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);

        ts.writeBarcodeToFile("after while sl ", "");
        // Responses from the server (code and message)

        try{
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();
        if(serverResponseCode==200)
        {
            ts.writeBarcodeToFile("in if server response ", "");
            //ts.writeBarcodeToFile("L S", ""); 
            tempFile1.delete();
        }
        else
        {
            ts.writeBarcodeToFile("in else server response ", "");
            ts.writeBarcodeToFile(serverResponseCode+"", "");

        }

        }
        catch(Exception e)
        {
            try{

                outputStream.flush();
                outputStream.close();
                }
                catch(Exception ex)
                {

                }

            ts.writeBarcodeToFile("in url catch response "+e.toString(), "");
            connection.disconnect();

        }

        ts.writeBarcodeToFile("after server  sl ", "");
        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        ts.writeBarcodeToFile("after close  sl ", "");
        }
        catch (java.net.SocketTimeoutException e) {
            try{
                fileInputStream.close();
            outputStream.flush();
            outputStream.close();
            }
            catch(Exception ex)
            {

            }
            ts.writeBarcodeToFile("in socketex  sl "+e.toString(), "");
            connection.disconnect();
            } 
        catch (java.io.IOException e) 
            {
            try{
                fileInputStream.close();
                outputStream.flush();
                outputStream.close();
                connection.disconnect();
                }
                catch(Exception ex)
                {

                }
            ts.writeBarcodeToFile("in ioex  sl "+e.toString(), "");

            }
        catch (Exception ex)
        {
            try{
                fileInputStream.close();
                outputStream.flush();
                outputStream.close();
                }
                catch(Exception exy)
                {

                }
            ts.writeBarcodeToFile("in catch  sl "+ex.toString(), "");
            connection.disconnect();
        //Exception handling
        }

        //System.gc();
        }




    }
    isRunning = false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:47:30+00:00Added an answer on June 17, 2026 at 8:47 pm

    This issue is related to the connectivity. i have also seen when we are on the wifi, we haven’t hit an unknown host exception error (except when I unplugged the device).
    We’re hitting unknown host exception errors when the device is solely on the 3g/4g.

    You can try resumable support for upload similar thread resume uploads using HTTP?

    or write your own protocol (chunk uploads).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send a file to a web service (ebridge) using their SendFile
I have an XML file which I need to parse using PHP and send
I need to send an apex web service some data. Then name of the
I need to upload some data to PHP server. I can do post with
In my code I am trying to send some data to my PHP file
I create a xhr in js to send some data to a php file.
Hello I have a jQuery function that will send to a php file some
I have an asp.net website that writes some data to a TXT file using
I am using FormToEmail.php free script to send some form data from a page
I've a custom form (created with form API) that need send an uploaded file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.