hello
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]>
>
> 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<http://ubergibson.com/%7Emicha/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;
> }
> }
> }
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---