Upload Files to Web Server

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

progress bar

Postby Boiler » Mon Nov 09, 2009 11:42 pm

Can anyone show example with progress bar of transfer data ?
Boiler
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Nov 09, 2009 11:35 pm

Top

Postby aswani » Sat Apr 17, 2010 5:23 am

hi all,


store image to server .client side code is ok and here i found server side is php.i am using the sqlserver db and .net at server side.if you have any idea about how to write server side code in asp.net can you please help me


Thanks in advance

Aswan
aswani
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Feb 09, 2010 12:41 pm

Re: java.io.FileNotFoundException

Postby Sambo » Mon May 17, 2010 12:57 am

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



I receiving the same error but only when I am not on a WiFi connection. When I am on a decent WiFi connection the file uploads fine using the php script. The file is written just the response from the server is fail, on the non-wifi connection. Any suggestions?
Sambo
Freshman
Freshman
 
Posts: 6
Joined: Mon May 17, 2010 12:49 am
Location: Seattle, WA

Re: Upload Files to Web Server

Postby Sambo » Tue May 18, 2010 8:07 am

A follow up on my last post, the uploads are failing when I am connected via the T-Mobile Edge network but uploads successfully while on 3G or a wifi connection. I noticed a few other apps unable to upload as well while on the Edge network so I am thinking this issue has to do with Tmobile Edge and not the code.
Sambo
Freshman
Freshman
 
Posts: 6
Joined: Mon May 17, 2010 12:49 am
Location: Seattle, WA

Re: Upload Files to Web Server

Postby BadGuy » Sat May 22, 2010 12:23 pm

Hi guys, i would like some help here

I have the following php web service:
Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. <?php
  2.         session_start();
  3.         include("nusoap.php");
  4.         require 'database.php';
  5.         $srv = new soap_server();
  6.  
  7.         $srv->register("insertMarker");
  8.         function insertMarker($facebookUid, $title, $description, $markerType, $photoPath, $latitude, $longitude)
  9.         {
  10.                
  11.                 $_SESSION["facebookUid"] = $facebookUid;
  12.  
  13.                 if(!file_exists("photos/".$facebookUid)) {
  14.                     mkdir("photos/".$facebookUid, 0777);
  15.                 }
  16.  
  17.                 global $mysqli;
  18.  
  19.                 $query = "INSERT into Markers(id,facebookUid, title, description, markerType, photoPath, latitude, longitude) VALUES('','$facebookUid','$title', '$description', '$markerType', '$photoPath', '$latitude', '$longitude')";
  20.  
  21.                 $result = $mysqli->query($query) or die (mysqli_error($mysqli));
  22.                 mysqli_close($mysqli);
  23.                 if($result) {
  24.                     return 'Successful insertion ';
  25.                 }
  26.                 else {
  27.                     return 'Something went wrong';
  28.                 }
  29.  
  30.         }
  31.         $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : "";
  32.         $srv->service($HTTP_RAW_POST_DATA);
  33. ?>
Parsed in 0.299 seconds, using GeSHi 1.0.8.4


which I am accessing from android using the soap protocol with the following code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  2.                   //SoapObject
  3.                  request.addProperty("facebookUid", data[0].get(0));
  4.                  request.addProperty("title", data[0].get(1));
  5.                  request.addProperty("description", data[0].get(2));
  6.                  request.addProperty("markerType", data[0].get(3));
  7.                  request.addProperty("photoPath", data[0].get(4));
  8.                  request.addProperty("latitude",data[0].get(5));
  9.                  request.addProperty("longitude", data[0].get(6));
  10.                  
  11.                  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  12.                  envelope.setOutputSoapObject(request);
  13.        
  14.                  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  15.                  try
  16.                  {
  17.                      androidHttpTransport.call(SOAP_ACTION, envelope);
  18.                      resultsRequestSOAP = envelope.getResponse();
  19.                      String result = (String) resultsRequestSOAP;
  20.                  }
  21.                  catch (Exception aE)
  22.                  {
  23.                   aE.printStackTrace ();;
  24.                  }
  25.  
Parsed in 0.215 seconds, using GeSHi 1.0.8.4


Then I am trying to upload a file to the web server from android. The php code for this is
Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. <?php
  2.         session_start();
  3.         $target_path = "photos/".$_SESSION["facebookUid"].'/';
  4.     $target_path = $target_path.basename($_FILES['uploadedfile']['name']);
  5.  
  6.     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  7.  
  8.         echo "The file ".basename($_FILES['uploadedfile']['name'])." has been uploaded to the folder ". $target_path;
  9.     }
  10.     else {
  11.  
  12.         echo "There was an error uploading the file, please try again!";
  13.     }
  14. ?>
  15.  
Parsed in 0.259 seconds, using GeSHi 1.0.8.4


and the java code is
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 HttpURLConnection connection = null;
  2.                 DataOutputStream outStream = null;
  3.                 DataInputStream inStream = null;
  4.        
  5.                 String lineEnd = "\r\n";
  6.                 String twoHyphens = "--";
  7.                 String boundary = "*****";
  8.        
  9.                 int bytesRead, bytesAvailable, bufferSize;
  10.        
  11.                 byte[] buffer;
  12.        
  13.                 int maxBufferSize = 1*1024*1024;
  14.        
  15.                 String urlString = "http://192.168.1.2/soap/uploadPhoto.php";
  16.                
  17.                 try {
  18.        
  19.                     FileInputStream fileInputStream = new FileInputStream(new File(photoPath));
  20.        
  21.                     // open a URL connection to the Servlet
  22.                     URL url = new URL(urlString);
  23.        
  24.                     // Open a HTTP connection to the URL
  25.                     connection = (HttpURLConnection) url.openConnection();
  26.        
  27.                     // Allow Inputs
  28.                     connection.setDoInput(true);
  29.        
  30.                     // Allow Outputs
  31.                     connection.setDoOutput(true);
  32.        
  33.                     // Don't use a cached copy.
  34.                     connection.setUseCaches(false);
  35.        
  36.                     // Use a post method.
  37.                     connection.setRequestMethod("POST");
  38.        
  39.                     connection.setRequestProperty("Connection", "Keep-Alive");
  40.                  
  41.                     connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  42.        
  43.                     outStream = new DataOutputStream(connection.getOutputStream());
  44.        
  45.                     outStream.writeBytes(twoHyphens + boundary + lineEnd);
  46.                     outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + photoPath +"\"" + lineEnd);
  47.                     outStream.writeBytes(lineEnd);
  48.        
  49.                     // create a buffer of maximum size
  50.                     bytesAvailable = fileInputStream.available();
  51.                     bufferSize = Math.min(bytesAvailable, maxBufferSize);
  52.                     buffer = new byte[bufferSize];
  53.        
  54.                     // read file and write it into form...
  55.        
  56.                     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  57.                    
  58.                         while (bytesRead > 0) {
  59.                                 outStream.write(buffer, 0, bufferSize);
  60.                         bytesAvailable = fileInputStream.available();
  61.                         bufferSize = Math.min(bytesAvailable, maxBufferSize);
  62.                         bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  63.                     }
  64.        
  65.                     // send multipart form data necesssary after file data...
  66.                         outStream.writeBytes(lineEnd);
  67.                         outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  68.        
  69.                     // close streams
  70.                     fileInputStream.close();
  71.                     outStream.flush();
  72.                     outStream.close();
  73.        
  74.                   }
  75.                   catch (MalformedURLException ex) {
  76.                           ex.printStackTrace();
  77.                   }
  78.        
  79.                   catch (IOException ioe) {
  80.                           ioe.printStackTrace();
  81.                   }
  82.        
  83.                   //------------------ read the SERVER RESPONSE
  84.                   try {
  85.                            inStream = new DataInputStream( connection.getInputStream() );
  86.                            String str;
  87.                            
  88.                            while (( str = inStream.readLine()) != null)
  89.                            {
  90.                                    Log.e("Upload photo","Server Response"+str);
  91.                            }
  92.                            inStream.close();
  93.        
  94.                   }
  95.                   catch (IOException ioex){
  96.                           ioex.printStackTrace();
  97.                   }
  98.  
Parsed in 0.194 seconds, using GeSHi 1.0.8.4


Everything works fine except from one thing. In my second php file the variable $_SESSION["facebookUid"] is null. I have tried many things but I can't figure out what the problem is. Does anyone have any idea? Any help would be appreciated! Sorry for my English but they are not very good...
BadGuy
Freshman
Freshman
 
Posts: 2
Joined: Thu May 13, 2010 10:16 am
Location: Patra

Re: Upload Files to Web Server

Postby aswani » Wed Jun 16, 2010 11:23 am

Hi Badguy

Thank you for your replay .my server side webservices is .net .i need help server side code .net or java is better

i don't know php.
aswani
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Feb 09, 2010 12:41 pm

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: pskink and 0 guests