| Author |
Message |
Surbhit Freshman

Joined: 17 Nov 2008 Posts: 4
|
Posted: Wed Nov 19, 2008 8:09 pm Post subject: |
|
|
method.getResponseBodyAsString() line is not working.
Is there any other way to get the response ??
Please help. |
|
| Back to top |
|
 |
|
|
 |
Nimrandir Once Poster

Joined: 16 May 2009 Posts: 1
|
Posted: Sun May 17, 2009 2:58 pm Post subject: Permission denied |
|
|
Hi all!
I followed this tutorial, but i get this error:
| Java: | 05-17 15:55:50.353: ERROR/3rd(690): java.net.SocketException: Permission denied (maybe missing INTERNET permission) |
The java code is the same posted by kat and also the file upload.php. I also created the folder uploads/ with all permission.
I work on Ubuntu 9.04. |
|
| Back to top |
|
 |
role123 Once Poster

Joined: 29 May 2009 Posts: 1
|
|
| Back to top |
|
 |
tweek3867 Once Poster

Joined: 03 Jun 2009 Posts: 1
|
Posted: Wed Jun 03, 2009 4:46 pm Post subject: |
|
|
On the Android phone you have to request permission to use the internet. Try adding the following to your Manifest file?
| Code: | | <uses-permission android:name="android.permission.INTERNET" /> |
|
|
| Back to top |
|
 |
jhoffman Freshman

Joined: 17 Jun 2009 Posts: 9
|
Posted: Wed Jun 17, 2009 2:07 am Post subject: |
|
|
| raquibulbari wrote: | This code worked for me, php code is same as posted before  |
I'm a bit thrown off by this, I've read through all of this thread, and looked at tutorials on the aspects that I struggled with. There were a lot of PHP samples given over the course of this thread. Could someone please summarize exactly which sample worked? Many people have posted snippets of the client side code which worked for them, but left out what PHP server side code worked. It would help me out a lot to see somebody post a pair of snippets which worked together to give them a result!
Thanks in advance for any help!  |
|
| Back to top |
|
 |
jhoffman Freshman

Joined: 17 Jun 2009 Posts: 9
|
Posted: Wed Jun 17, 2009 8:19 pm Post subject: |
|
|
I actually solved my own problem after about a week of agonizing over it, but for any future readers of this thread, here are the two pieces of the puzzle, put together, with me saying that it worked for me:
Java code (as previously posted in this thread -- this is not my work):
| Java: | import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestUpload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = "/sdcard/yourfile.jpg";
// Is this the place are you doing something wrong.
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://localhost/uploader.php";
try
{
//------------------ CLIENT REQUEST
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.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");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e("MediaPlayer","Headers are written");
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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
Log.e("MediaPlayer","File is written");
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("MediaPlayer","Server Response"+str);
}
inStream.close();
}
catch (IOException ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}
}
}
|
The PHP server-side (again, posted earlier in this thread, and not my work, but these two never appear in one single post saying that they work together):
| Code: | <?php
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$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!";
}
?> |
The two things you need to set up in the Java file are: urlString and existingFileName. Configure these such that they match whatever you name your PHP file, and whatever file you're trying to upload. Keep in mind that (as previously posted) you must manually push said file to your emulator's pseudo-SD Card for this to work. (Or have it on a real physical SD card if testing on a real phone). I hope this clears up any confusion that other readers like myself may have in the future! |
|
| Back to top |
|
 |
|
|
 |
llPorZall Freshman

Joined: 09 Sep 2008 Posts: 5
|
Posted: Fri Jun 19, 2009 5:09 pm Post subject: |
|
|
Help Me!! I Create Project ,Take a picture upload picture to Server cannot Work
| Java: |
public class ImageCaptureCallback implements PictureCallback {
protected final static String TAG = "PUPA";
private OutputStream filoutputStream;
private HttpURLConnection connection;
private DataOutputStream dataOutputStream;
private DataInputStream inputStream;
int bytesRead, bytesAvailable, bufferSize;
int maxBufferSize = 2 * 1024 * 1024;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
byte[] buffer;
public ImageCaptureCallback(OutputStream filoutputStream) {
this.filoutputStream = filoutputStream;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Log.v(TAG, "onPictureTaken=" + data + " length = " + data.length);
filoutputStream.write(data);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(data);
// Log.v(TAG,"Read Data:"+fileInputStream.read());
URL url = new URL("http://172.30.142.173/upload/");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Use a post method.
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;");
dataOutputStream = new DataOutputStream(connection
.getOutputStream());
dataOutputStream
.writeBytes("Content-Disposition: form-data; name=\"xml\""
+ lineEnd + lineEnd);
bytesAvailable = arrayInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
Log.v(TAG,"Buffer Size:"+bufferSize);
buffer = new byte[bufferSize];
bytesRead = arrayInputStream.read(buffer, 0, bufferSize);
Log.v(TAG, "connect to server:" + connection.getResponseMessage());
while (bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = arrayInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = arrayInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
Log.e(TAG, "File is written");
/*
* fileInputStream.close(); dataOutputStream.flush();
* dataOutputStream.close(); filoutputStream.flush();
* filoutputStream.close();
*/
} catch (MalformedURLException ex) {
Log.v(TAG, "Ex:" + ex);
} catch (IOException e) {
Log.v(TAG, "Ex:" + e);
}
try {
inputStream = new DataInputStream(connection.getInputStream());
String str;
while ((str = inputStream.readLine()) != null) {
Log.v(TAG, "Server Response:" + str);
}
inputStream.close();
} catch (IOException e) {
Log.v(TAG, "Ex:" + e);
}
}
}
|
PHP Page
| Code: | <?php
// Where the file is going to be placed
$target_path = "/upload/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$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 againg!";
}
?> |
Output
There was an error uploading the file, please try againg!
Thanks very much |
|
| Back to top |
|
 |
jhoffman Freshman

Joined: 17 Jun 2009 Posts: 9
|
Posted: Fri Jun 19, 2009 7:10 pm Post subject: |
|
|
This is a total shot in the dark, but is it possible that this:
| Java: | $target_path = "/upload/"; |
actually needs to be... this:
| Java: | $target_path = "upload/"; | ?
In my code, I don't have a slash preceding the upload directory. I'm wondering if perhaps your operating system thinks you're trying to access a directory like var/html/www//upload
instead of var/html/www/upload
I actually had a similar problem with my code, except it was as a result of me forgetting the put the slash AFTER upload, maybe this is your problem? |
|
| Back to top |
|
 |
llPorZall Freshman

Joined: 09 Sep 2008 Posts: 5
|
Posted: Sat Jun 20, 2009 8:31 am Post subject: |
|
|
| jhoffman wrote: | This is a total shot in the dark, but is it possible that this:
| Java: | $target_path = "/upload/"; |
actually needs to be... this:
| Java: | $target_path = "upload/"; | ?
In my code, I don't have a slash preceding the upload directory. I'm wondering if perhaps your operating system thinks you're trying to access a directory like var/html/www//upload
instead of var/html/www/upload
I actually had a similar problem with my code, except it was as a result of me forgetting the put the slash AFTER upload, maybe this is your problem? |
Thanks very much
I change PATH to:
| Code: | | $target_path = "upload/"; |
output:There was an error uploading the file, please try againg!
i create html form for testing php upload page but it's doesn't work!
So I want to know there have any wrong code in the Java part
| Java: | public class ImageCaptureCallback implements PictureCallback {
protected final static String TAG = "PUPA";
private OutputStream filoutputStream;
private HttpURLConnection connection;
private DataOutputStream dataOutputStream;
private DataInputStream inputStream;
int bytesRead, bytesAvailable, bufferSize;
int maxBufferSize = 2 * 1024 * 1024;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
byte[] buffer;
private String string = "/sdcard/DCIM/100media/IMAG0001.jpg";
public ImageCaptureCallback(OutputStream filoutputStream) {
this.filoutputStream = filoutputStream;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Log.v(TAG, "onPictureTaken=" + data + " length = " + data.length);
filoutputStream.write(data);
FileInputStream arrayInputStream = new FileInputStream(new File(string));
// Log.v(TAG,"Read Data:"+fileInputStream.read());
URL url = new URL("http://172.30.142.173/upload/uploader.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Use a post method.
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dataOutputStream = new DataOutputStream(connection
.getOutputStream());
dataOutputStream
.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ "IMAG0001.jpg" + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
bytesAvailable = arrayInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
Log.v(TAG, "Buffer Size:" + bufferSize);
buffer = new byte[bufferSize];
bytesRead = arrayInputStream.read(buffer, 0, bufferSize);
Log.v(TAG, "connect to server:" + connection.getResponseMessage());
while (bytesRead > 0) {
Log.v(TAG, "Read:" + bytesRead);
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = arrayInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = arrayInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
Log.e(TAG, "File is written");
arrayInputStream.close();
dataOutputStream.flush();
} catch (MalformedURLException ex) {
Log.v(TAG, "Ex:" + ex);
} catch (IOException e) {
Log.v(TAG, "Ex:" + e);
}
try {
inputStream = new DataInputStream(connection.getInputStream());
String str;
while ((str = inputStream.readLine()) != null) {
Log.v(TAG, "Server Response:" + str);
}
dataOutputStream.close();
} catch (IOException e) {
Log.v(TAG, "Ex:" + e);
}
}
}
|
|
|
| Back to top |
|
 |
jhoffman Freshman

Joined: 17 Jun 2009 Posts: 9
|
Posted: Sat Jun 20, 2009 7:56 pm Post subject: |
|
|
Do you mean that when you test your PHP with your HTML form, it does work or does not work?
If your html form is also failing, I would say the problem is with your PHP. Just from glancing over your code I didn't see anything obviously wrong with it, but I am VERY new to this type of code, and not really going to be super useful at finding intricacies that are wrong.
What I would recommend is that you narrow down exactly where in your code things start to go wrong. If you use eclipse, set up some breakpoints so you can peek at your variables in debug mode. If not, use the log.e method call to output contents of your variables in a bunch of places along the code's execution. Look at these using the Log tool (part of DDMS or can be run stand-alone I believe).
When I had trouble with my http code, I was able to narrow things down in this way and determined that my specific problem was actually related to the file access on my Android and NOT any of the web stuff. No real guarantee that you'll be that lucky, but narrowing down the problem always helps! |
|
| Back to top |
|
 |
llPorZall Freshman

Joined: 09 Sep 2008 Posts: 5
|
Posted: Sat Jun 20, 2009 8:30 pm Post subject: |
|
|
| jhoffman wrote: | Do you mean that when you test your PHP with your HTML form, it does work or does not work?
If your html form is also failing, I would say the problem is with your PHP. Just from glancing over your code I didn't see anything obviously wrong with it, but I am VERY new to this type of code, and not really going to be super useful at finding intricacies that are wrong.
What I would recommend is that you narrow down exactly where in your code things start to go wrong. If you use eclipse, set up some breakpoints so you can peek at your variables in debug mode. If not, use the log.e method call to output contents of your variables in a bunch of places along the code's execution. Look at these using the Log tool (part of DDMS or can be run stand-alone I believe).
When I had trouble with my http code, I was able to narrow things down in this way and determined that my specific problem was actually related to the file access on my Android and NOT any of the web stuff. No real guarantee that you'll be that lucky, but narrowing down the problem always helps! |
Thanks very much MR.jhoffman
I'm Sending file to server is success |
|
| Back to top |
|
 |
jhoffman Freshman

Joined: 17 Jun 2009 Posts: 9
|
Posted: Sun Jun 21, 2009 2:44 am Post subject: |
|
|
Congrats! I'm glad to hear it!
Are you able to send from an actual GPhone, or just the emulator? I'm having some errors sending from my Ion phone, but the emulator works flawlessly. I get file not found errors as described in the thread I made
Let me know if you are able to send from your phone as well as the emulator, I'd be interested to learn if I've done something strange to stop my program from working on the real phone! |
|
| Back to top |
|
 |
draviaz Freshman

Joined: 10 Aug 2009 Posts: 2
|
Posted: Sun Aug 16, 2009 2:31 pm Post subject: an equivalent in java servlet for the php uploader |
|
|
| Hi, I would like to use java servlets instead of php for managing the server-side upload. Can somebody help? |
|
| Back to top |
|
 |
jbrohan Freshman

Joined: 28 Aug 2009 Posts: 8 Location: Montreal
|
Posted: Fri Aug 28, 2009 11:20 am Post subject: This worked for me too...here is html test harness |
|
|
First of all I would like to thank those who added to this most valuable resource. My project is moving ahead now. This is a minimalist html script which will test TestUpload.php in the same web folder. You can run this form from a browser!
Good luck and Blessings to all
Thanks again.
John
<form enctype='multipart/form-data' action='TestUpload.php' method='POST'>
<!-- MAX_FILE_SIZE must precede the file input field -->
Phone: <input type = 'text' name = 'phone' value = ''/>
<input type='hidden' name='MAX_FILE_SIZE' value='10000000' />
<!-- Name of input element determines name in $_FILES array -->
<br/>Search for the picture.<input name='uploadedfile' type='file' size= '50' />
<br/>
<br/>
<input type='submit' value='Send in picture'/>
</form> |
|
| Back to top |
|
 |
sng2392 Once Poster

Joined: 20 Oct 2009 Posts: 1
|
Posted: Tue Oct 20, 2009 12:36 am Post subject: java.io.FileNotFoundException |
|
|
Does anyone have an idea to why I get this error?
10-19 23:02:06.141: ERROR/MediaPlayer(899): error: http://www.webhostsomewhere.com/SomeFolder/upload.aspx
10-19 23:02:06.141: ERROR/MediaPlayer(899): java.io.FileNotFoundException: http://www.webhostsomewhere.com/SomeFolder/upload.aspx
10-19 23:02:06.141: ERROR/MediaPlayer(899): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.vtind.guesswho.ImageDownload.uploadFile(ImageDownload.java:217)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.vtind.guesswho.SendView$2.onClick(SendView.java:85)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.View.performClick(View.java:2179)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.View.onTouchEvent(View.java:3828)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.widget.TextView.onTouchEvent(TextView.java:6291)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.View.dispatchTouchEvent(View.java:3368)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.os.Looper.loop(Looper.java:123)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at android.app.ActivityThread.main(ActivityThread.java:3948)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at java.lang.reflect.Method.invoke(Method.java:521)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
10-19 23:02:06.141: ERROR/MediaPlayer(899): at dalvik.system.NativeStart.main(Native Method)
My client side code is identical to what has been posted by jhoffman |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
© 2007, Android Development Community
All rights reserved.
Powered by phpBB.
|