ANDROID upload FILE to server PROBLEM

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

ANDROID upload FILE to server PROBLEM

Postby breakenter » Mon Jun 18, 2012 10:10 am

Hi boys. I am a beginner in programming Android java (eclipse).
Ihave Android tablet Asus SL android vr 3.2

I need upload TXT file from SD card to web server.
I seachr on internet very many guides how to upload file to server, but in real machine NOT work.


I use this Androin side this CODE :

public void doFileUpload() throws FileNotFoundException
{
String existingFileName = "/sdcard/Download/test.txt";
File uploadFile = new File(existingFileName);
FileInputStream fileInputStream = new FileInputStream(uploadFile);

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL connectURL = new URL("http://mywebpage/uploads.php");
// connectURL is a URL object
HttpURLConnection conn = (HttpURLConnection)
connectURL.openConnection();

// allow inputs
conn.setDoInput(true);

// allow outputs
conn.setDoOutput(true);

// don't use a cached copy
conn.setUseCaches(false);

// use a post method
conn.setRequestMethod("POST");

// set post headers
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);


// open data output stream
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

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

// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];

// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

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

// close streams
fileInputStream.close();
dos.flush();

InputStream is = conn.getInputStream();
int ch;

StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
b.append( (char)ch );
}

// String s=b.toString();
dos.close();

}


catch (MalformedURLException ex) {
// Log.e(Tag, "error: " + ex.getMessage(), ex);
mInfoBox.setText("error: " + ex.getMessage());

}
catch (IOException ioe) {
// Log.e(Tag, "error: " + ioe.getMessage(), ioe);
mInfoBox.setText("error: " + ioe.getMessage());
}

}


//=======================================================================================

but at this line
the application on a real machine totally crash !!

// open data output stream
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());



//=======================================================================================

In manifest XLM have this permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

//=======================================================================================

At side server have this PHP code + create directory "uploads" with 777 permission.

<?php
$target_path = "uploads/";

$target_path = $target_path.basename($_FILES['uploadedfile']
['name']);

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
$target_path))
{
echo "The file ".basename($_FILES['uploadedfile']['name'])." has been
uploaded";
}

else
{
echo "There was an error uploading the file, please try again!";
}

?>
//=======================================================================================

PHP code is tested at server and work OK.
the problem is on the side JAVA-Android.

Does anyone have working code ? Upload file to PHP server ( real machine + real WEB page )
(Or other JAVA code + other PHP code (import APACHE .etc) )


I found this note on the Internet :
I reccomend to insert this code in Thread because it's possibile that the Android system will kill your app for to intensive CPU use.


Can someone rewrite the code to work in Thread ?
Thank you for your help in solving the problem UPLOAD txt file to PHP server.
(sorry for bad English)
breakenter
Freshman
Freshman
 
Posts: 2
Joined: Sun Jun 17, 2012 9:23 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests