-2

I'm a google glass developer. I would like to upload the photos from my glass memory to a php server using WinSCP. This this the current code:

private void postFile(){
        try{
            // the file to be posted
            String textFile = Environment.getExternalStorageDirectory() + "/DCIM/Camera/test.txt";
            Log.v(TAG, "textFile: " + textFile);
            // the URL where the file will be posted
            String postReceiverUrl = "http://learningzone.me/build/course/en/roi.php";
            Log.v(TAG, "postURL: " + postReceiverUrl);
            // new HttpClient
            HttpClient httpClient = new DefaultHttpClient();
            // post header
            HttpPost httpPost = new HttpPost(postReceiverUrl);
            File file = new File(textFile);
            FileBody fileBody = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("file", fileBody);
            httpPost.setEntity(reqEntity);
            // execute HTTP post request
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                String responseStr = EntityUtils.toString(resEntity).trim();
                Log.v(TAG, "Response: " +  responseStr);
                // you can add an if statement here and do other actions based on the response
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

and this it the script for roi.php on the server:

$host = "ftp.example.com";
$user = "anonymous";
$pass = "";

// You get this from the form, so you don't need to do move_uploaded_file()
$fname = "/public_html/new_file.txt";
$fcont = "content";

function ftp_writeFile($ftp, $new_file, $content, $debug=false) {
    extract((array)pathinfo($new_file));

    if (!@ftp_chdir($ftp, $dirname)) {
        return false;
    }

    $temp = tmpfile();
    fwrite($temp, $fcont);
    rewind($temp);  

    $res = @ftp_fput($ftp, $basename, $temp, FTP_BINARY);
    if ($debug) echo "a- '$new_file'".(($res)?'':" [error]")."<br/>";
    fclose($temp);

    return $res;
}

$ftp = ftp_connect($host);
if (!$ftp) echo "Could not connect to '$host'<br/>";

if ($ftp && @ftp_login($ftp, $username, $password)) {
    ftp_writeFile($ftp, $fname, $fcont, true);
} else {
    echo "Unable to login as '$username:".str_repeat('*', strlen($password))."'<br/>";
}

ftp_close($ftp);

Does anyone know what's the problem? Thanks!

Roi Bueno
  • 99
  • 10
  • 2
    How are we supposed to know what the problem is when you don't even tell us the error? – timgeb Jun 09 '14 at 09:36
  • At the moment, it looks like you're trying to write the string "content" to a file. You need to actually access the POSTed image file using `$fcont = $_FILES["file"];`. Take a look at PHP file uploading here: http://www.w3schools.com/php/php_file_upload.asp – Liam George Betsworth Jun 09 '14 at 09:41
  • The error is that it doesn't upload the needed file. In eclipse: "thread exiting with uncaught exception (group=0x416c0bd8); FATAL EXCEPTION: AsyncTask #2;.." – Roi Bueno Jun 09 '14 at 09:45
  • More specifically: When using the code in this link: http://www.codeofaninja.com/2013/04/android-http-client.html in order to upload images from google glass memory to a php server, I get the following error: http://postimg.org/image/qzup71qkl/ What am I doing wrong? – Roi Bueno Jun 09 '14 at 10:25
  • You're not even using WinSCP in the code you've posted, so tagging it and the title is misleading. – AeroX Jun 25 '14 at 14:40

1 Answers1

2

You need to use Async task to spawn off uploading to a different thread. Android works on a single thread model and using the same thread to make HTTPRequest can result in FATAL exception. Create an Async task and spawn off the upload to it.

AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(<pass the required parameters here for file upload>);

private class AsyncTaskRunner extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            //Call the function to upload the file here

        }
G3M
  • 1,023
  • 8
  • 19