I wanted to upload a file from android device to a server using PHP post. Now I researched on the multipart entity uploading and fixed everything on my android device but it's still not working and I just don't know why since this is the first time I'd tried to do this for android. Now what I have is this method:
public void postFile(String file_path, String url){
File file = new File(file_path);
try {
System.out.println(file_path);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bin); /*I believe that the "file" is the name in php part*/
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
and I use an onclick listener to call this method with this format
postFile(record_location, "http://10.146.179.86/File%20upload/test.php");
where record location is this : /mnt/sdcard/Echo/Recordings/Awesome4.3gpp
and the PHP upload code:
<?php
$target_path = "Stored File/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
I already tried the upload and it's working which is why I think the problem is in my android method. By the way here's the logcat:
06-20 06:38:39.662 37-88/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-20 06:38:39.912 278-294/system_process I/ActivityManager: Displayed com.obpg.echo/.PlaybackEditActivity: +704ms
06-20 06:38:41.933 3399-3399/com.obpg.echo I/System.out: /mnt/sdcard/Echo/Recordings/Awesome4.3gpp
06-20 06:38:42.132 3399-3399/com.obpg.echo W/System.err: android.os.NetworkOnMainThreadException
06-20 06:38:42.213 3399-3399/com.obpg.echo W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:112)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.Socket.connect(Socket.java:842)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-20 06:38:42.243 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-20 06:38:42.243 3399-3403/com.obpg.echo D/dalvikvm: GC_CONCURRENT freed 347K, 12% free 3688K/4156K, paused 77ms+103ms, total 254ms
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at com.obpg.echo.PlaybackEditActivity.postFile(PlaybackEditActivity.java:279)
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at com.obpg.echo.PlaybackEditActivity$3.onClick(PlaybackEditActivity.java:126)
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at android.view.View.performClick(View.java:4204)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.view.View$PerformClick.run(View.java:17355)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Handler.handleCallback(Handler.java:725)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Looper.loop(Looper.java:137)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at dalvik.system.NativeStart.main(Native Method)
Did I missed out something here? Also maybe someone can explain me on how does this entity.addPart() works since since I dont know what is the content body it talking about and what string should I specify.