

sng2392 wrote: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







package com.test.upload;
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);
doFileUpload();
}
private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = "/sdcard/a.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://www.myserver.com/testupload.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("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);
}
}
}
jhoffman wrote: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):Using java Syntax Highlighting
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 = "rn"; 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); } } } Parsed in 0.054 seconds, using GeSHi 1.0.8.4
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: Select all
<?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!

venkat wrote:Dear Katharnavas. Thanks a lot. it works...![]()
![]()
![]()
![]()
Regards,![]()
Venkat..![]()
![]()
<?php
mysql_connect("mysql8.000webhost.com", "a3603099_drikvy","drikvyx86");
mysql_select_db("a3603099_android");
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
?>





01-05 13:40:34.618: ERROR/Got an IOException:(4721): java.net.SocketException: The operation timed out
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at java.net.Socket.connect(Socket.java:1055)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:1253)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at net.Upload.Upload.onCreate(Upload.java:55)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.os.Looper.loop(Looper.java:144)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at android.app.ActivityThread.main(ActivityThread.java:4937)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at java.lang.reflect.Method.invoke(Method.java:521)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-05 13:40:34.618: ERROR/Got an IOException:(4721): at dalvik.system.NativeStart.main(Native Method)
outputStream = new DataOutputStream( connection.getOutputStream() );


Users browsing this forum: No registered users and 6 guests