Question

I have written out a program which is designed to upload text files created from motion data information to a PHP cloud server that I have set up with 000webhost.com. Ive tried running it however it will not run for some reason. None of the sensor information is uploaded and there seems to be a problem me thinks on the server end. Is the error in the script or code? (or both?).

Edit: Yeah I know its not "great", I am still fairly new to coding and been trying to fiddle around with the code for a while (still working on it).

Program

package com.example.motionsense3;

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimerTask;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.DataOutputStream;
import java.io.File;
import android.util.Log;


public class SaveTimer extends TimerTask
{
    String upLoadServerUri = null;
    final String uploadFilePath = "/data/data/com.example.motionsense3/";
    //final String uploadFileName = "????";

    private ArrayList<String> motionData;
    private ArrayList<String> motionDataClone;
    private Context context;

    public SaveTimer(ArrayList<String> motionData, MainActivity context) {
        this.motionDataClone = (ArrayList<String>) motionData.clone();
        this.motionData = motionData;
        this.context = context;
    }

    @Override
    public void run() {
        Log.e("DIDUPLOADWORK", Boolean.toString(upload()));

    }

    private void save() {
         try {
             this.motionDataClone = (ArrayList<String>) motionData.clone();
             motionData.clear();

             FileOutputStream fileOutput = context.openFileOutput("scaninfo_" + new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss").format(new Date()).toString(), context.MODE_WORLD_READABLE);//.write(s);
             for(String s : motionDataClone)
             {
                 fileOutput.write(s.getBytes());
             }
             fileOutput.close();


         } catch (Exception e) {

         }

        //save();

}


         public boolean upload(){//ArrayList<String> motion){//String sourceFileUri) {

             /************* Php script path ****************/
             upLoadServerUri = "motionsense.uphero.com/public_html/motiondata/UploadToServer.php";

          //String fileName = sourceFileUri;

          HttpURLConnection conn = null;
          DataOutputStream dos = null;  
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024; 


               try { 

                   URL url = new URL(upLoadServerUri);
                    String fileName = "scaninfo_" + new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss").format(new Date()).toString();
                   // Open a HTTP  connection to  the URL
                   conn = (HttpURLConnection) url.openConnection(); 
                   conn.setDoInput(true); // Allow Inputs
                   conn.setDoOutput(true); // Allow Outputs
                   conn.setUseCaches(false); // Don't use a Cached Copy
                   conn.setRequestMethod("POST");
                   conn.setRequestProperty("Connection", "Keep-Alive");
                   conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                   conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                   conn.setRequestProperty("uploaded_file", fileName); 

                   dos = new DataOutputStream(conn.getOutputStream());

                   dos.writeBytes(twoHyphens + boundary + lineEnd); 
                   dos.writeBytes("Content-Disposition: form-data; name=uploaded_file; filename="
                                             + fileName + "" + lineEnd);

                   dos.writeBytes(lineEnd);


                   for(String s : motionDataClone)
                   {
                       dos.write(s.getBytes());
                   }

                   // send multipart form data necesssary after file data...
                   dos.writeBytes(lineEnd);
                   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                   // Responses from the server (code and message)
                   int serverResponseCode = conn.getResponseCode();
                   String serverResponseMessage = conn.getResponseMessage();
                   Log.e("SERVERRESPONSE", serverResponseMessage);
                   Log.e("SERVERRESPONSECODE", String.valueOf(serverResponseCode));

               }
               catch(Exception e){return false;}
               return true;
         }
    }

Php script

<?php

    $file_path = "motiondata/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>
Was it helpful?

Solution

Just quickly glancing through the code, I believe your problem is here:

upLoadServerUri = "motionsense.uphero.com/public_html/motiondata/UploadToServer.php";

Basically you're trying to make a connection to UploadToServer.php through this public_html directory, however, this directory doesn't exist publically (it's where your "root" files are served from when accessing a website with '/'). Change to:

upLoadServerUri = "motionsense.uphero.com/motiondata/UploadToServer.php";

And you should be fine (assuming everything else works haha).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top