Example: upload files to php from android

by octagon » Wed, 22 Oct 2008 09:25:08 GMT


Sponsored Links
 his 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);



Example: upload files to php from android

by De San Nicolas Jean Philippe » Fri, 24 Oct 2008 05:45:49 GMT


 ello

just a question (for the moment -:))

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

thank's


2008/10/21 octagon <[EMAIL PROTECTED]>


--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Example: upload files to php from android

by octagon » Sat, 20 Dec 2008 03:20:37 GMT


 orry, 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:45pm, "De San Nicolas Jean Philippe" <jph...@gmail.com>
wrote:
--~--~---------~--~----~------------~-------~--~----~



Example: upload files to php from android

by arnouf » Mon, 19 Jan 2009 20:57:09 GMT


 i 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-php-server.html

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:
--~--~---------~--~----~------------~-------~--~----~



Example: upload files to php from android

by arnouf » Mon, 26 Jan 2009 17:35:33 GMT


 t's done :)

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



Example: upload files to php from android

by AKD » Sat, 07 Mar 2009 10:53:19 GMT


 i arnouf could you plz provide your code. i need to work on same
task. thanks in advance.

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

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. allign text vertically in a text view

I have a textview placed vertically in an activity.. I Want the text
within the textview to be shown as bottom to top facing right side.
Can anyone please help me out..........
my XML file is

<TextView
    android:layout_height="200dp"
    android:layout_width="50dp"
    android:paddingTop="100dp"
    android:text="ALL"
    android:textSize=" 20px"
    android:autoText="true"
     android:paddingLeft="5dp"
     android:layout_gravity="fill_vertical"
  />

-- 

2. Extending multiple classes

Thanks guys for the quick response.
Will try it out and let you know

Thanks
Priyank






> 

3. How to solve this problem: Close Force Error when trying to populate viewStub

4. second activity error onClick package is duplicating in intent line

5. nexus one dan htc desir

6. Nubie Perkenalan + WTA HTC Magic

7. Android with S3?