dongtuoji5396 2013-06-20 06:52
浏览 100
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • douliang7068 2013-06-20 07:32
    关注

    Taken from my comment:

    android.os.NetworkOnMainThreadException says that you're posting on UI thread. Call postFile() either from an AsynkTask either from a background thread.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效