使用Android中的MultipartEntity上传3gpp音频文件

时间:2013-06-20 06:52:53

标签: java php android

我想使用PHP帖子将文件从Android设备上传到服务器。现在我研究了多部分实体上传并修复了我的Android设备上的所有东西,但它仍然无法工作,我只是不知道为什么,因为这是我第一次尝试为Android做这个。我现在拥有的是这种方法:

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();
        }
    }

我使用onclick监听器以这种格式调用此方法

postFile(record_location, "http://10.146.179.86/File%20upload/test.php");

其中记录位置为:/mnt/sdcard/Echo/Recordings/Awesome4.3gpp

和PHP上传代码:

<?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!";
}

?>

我已经尝试过上传并且它正在工作,这就是为什么我认为问题出在我的android方法中。顺便说一下这里是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)

我错过了什么吗? 也许有人可以解释我这个entity.addPart()是如何工作的,因为我不知道它所讨论的内容主体是什么,我应该指定什么字符串。

1 个答案:

答案 0 :(得分:2)

取自我的评论:

android.os.NetworkOnMainThreadException说您在UI线程上发帖。从后台线程中的AsynkTask中调用postFile()