Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Example: upload files to php from android
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
octagon  
View profile  
(1 user)  More options Oct 21 2008, 2:53 pm
From: octagon <Micha.Nis...@gmail.com>
Date: Tue, 21 Oct 2008 11:53:19 -0700 (PDT)
Local: Tues, Oct 21 2008 2:53 pm
Subject: Example: upload files to php from android
This is a way for android to POST a file upload to a php script. I had
a bit of trouble figuring out the ins and outs of the http client
situation, but this is what works for me (hope someone finds it
helpful):

Notes:

Expect/continue handshaking needed to be disabled to avoid getting 417
errors from lighttpd. Doesn't work without an sdcard yet, as there is
no Content-Length header associated with uploading an OutputStream as
opposed to a File object, and writing to files is only allowed on the
sdcard (as far as I know, please correct me if there is a way to do
this).

Dependencies:

apache-mime4j-0.5.jar
log4j-zeroconf.jar
httpmime-4.0-beta1.jar

upload.php:

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /


<input type="submit" value="Upload File" />
</form>
<?php
  $to_file = "tmp/" . basename($_FILES['uploadedfile']['name']);
  $from_file = $_FILES['uploadedfile']['tmp_name'];

  if (move_uploaded_file($from_file, $to_file)) {
    echo "Successful upload";
?>
  <a href="<?php echo $to_file;?>"><?php echo $to_file;?></a>
<?php
  } else {
    echo "Unsuccessful upload";
  }
?>

DemoActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class DemoActivity extends Activity {

  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Make a simple view with a button and a bit of text. Click
the button to upload the file to the
         * server. The file will be saved to tmp/test.txt (relative to
your php script) and it should contain
         * the current time and date.
         */
        final TextView tmp = (TextView) findViewById(R.id.textView1);
        tmp.setText("Hi! Click the button!");

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        File f = new File("/sdcard/test.txt");
                        try {
                                        f.createNewFile();
                                        Date d = new Date();
                                        PrintWriter writer = new PrintWriter(f);
                                        writer.println(d.toString());
                                        writer.close();

                                        HttpClient client = new DefaultHttpClient();
                                        httpPostFileUpload(client, "/sdcard/test.txt", "http://
ubergibson.com/~micha/work/oculi/upload.php", "uploadedfile");
                        } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                }
        });
    }

    /**
     * Upload a file using a POST request.
     *
     * @param client the HTTP client object
     * @param filePath local file location
     * @param uploadUri URI to POST to
     * @param inputNameAttr the name attribute of the file input
element in
     * the html form
     * @throws IOException
     * @throws ClientProtocolException
     */
    public void httpPostFileUpload(
                HttpClient client,
                String filePath,
                String uploadUri,
                String inputNameAttr) throws ClientProtocolException,
IOException {

        HttpUriRequest  request         = new HttpPost(uploadUri);
        MultipartEntity form            = new MultipartEntity();

        // disable expect-continue handshake (lighttpd doesn't support
it)
        client.getParams().setBooleanParameter(
                        "http.protocol.expect-continue", false);

        form.addPart(inputNameAttr, new FileBody(new File(filePath)));

                ((HttpEntityEnclosingRequestBase) request).setEntity(form);

                try {
                        client.execute(request);
                } catch (ClientProtocolException e) {
                        throw e;
                } catch (IOException ee) {
                        throw ee;
                }
    }


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
De San Nicolas Jean Philippe  
View profile  
 More options Oct 23 2008, 5:45 pm
From: "De San Nicolas Jean Philippe" <jph...@gmail.com>
Date: Thu, 23 Oct 2008 23:45:22 +0200
Local: Thurs, Oct 23 2008 5:45 pm
Subject: Re: [android-developers] Example: upload files to php from android

hello

just a question (for the moment -:))

you set the dependencies in your classpath in your Android project?

thank's

2008/10/21 octagon <Micha.Nis...@gmail.com>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
octagon  
View profile  
 More options Dec 19 2008, 2:20 pm
From: octagon <Micha.Nis...@gmail.com>
Date: Fri, 19 Dec 2008 11:20:28 -0800 (PST)
Local: Fri, Dec 19 2008 2:20 pm
Subject: Re: Example: upload files to php from android
Sorry, I didn't see your question! In case anyone has the same
problem, the answer is yes. I am using eclipse. I first imported the
jar files into my project so that the whole thing is all in one place.
I put them in res/assets/dependencies.
Then do:

1. main menu Project --> Properties
2. select Java Build Path from left pane
3. go to Libraries tab
4. click Add JARs

hope it helps someone out.

On Oct 23, 4:45 pm, "De San Nicolas Jean Philippe" <jph...@gmail.com>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
arnouf  
View profile  
 More options Jan 19, 7:56 am
From: arnouf <arnaud.far...@gmail.com>
Date: Mon, 19 Jan 2009 04:56:55 -0800 (PST)
Local: Mon, Jan 19 2009 7:56 am
Subject: Re: Example: upload files to php from android
Hi all,

I used this method until now. But I would like to light my application
on device - without apache libraries - to use the code present in this
page
http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-p...

It doesn't work...Could you help me?

For information I would like to send an xml file (so text) and not a
binary.

Regards

On Dec 19 2008, 8:20 pm, octagon <Micha.Nis...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
arnouf  
View profile  
 More options Jan 26, 4:35 am
From: arnouf <arnaud.far...@gmail.com>
Date: Mon, 26 Jan 2009 01:35:18 -0800 (PST)
Local: Mon, Jan 26 2009 4:35 am
Subject: Re: Example: upload files to php from android
It's done :)

On 19 jan, 13:56, arnouf <arnaud.far...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AKD  
View profile  
 More options Mar 5, 5:40 pm
From: AKD <dimriash...@gmail.com>
Date: Thu, 5 Mar 2009 14:40:35 -0800 (PST)
Local: Thurs, Mar 5 2009 5:40 pm
Subject: Re: Example: upload files to php from android
hi arnouf could you plz provide your code. i need to work on same
task. thanks in advance.

On Jan 26, 2:35 pm, arnouf <arnaud.far...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google