douchui3933 2014-05-09 14:02
浏览 32
已采纳

Android应用发布everyThing除了发布的文件

i use apache-mime4j-0.6.jar and httpmime-4.0.1.jar , in my Logcat Everything is Good except this Log out_write() limiting sleep time 31178 to 23219 with tag audio_hw_primary but I'm not sure it's From my App.

Problem : in my php File , I can Receive Everything except My posted file !

this is my Code , base on this Question

package com.negano.Uploader;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


public class ActivityMain extends Activity {

    private static DefaultHttpClient mHttpClient;


    public static void ServerCommunication() {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        mHttpClient = new DefaultHttpClient(params);
    }


    public void uploadUserPhoto(File image) {

        try {

            HttpPost httppost = new HttpPost("http://logcat.ir/uproid.php");

            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntity.addPart("Title", new StringBody("Title"));
            multipartEntity.addPart("Nick", new StringBody("Nick"));
            multipartEntity.addPart("Email", new StringBody("Email"));
            multipartEntity.addPart("Image", new FileBody(image));
            httppost.setEntity(multipartEntity);

            HttpResponse result = mHttpClient.execute(httppost);
            InputStream stream;
            stream = result.getEntity().getContent();
            String response = inputstreamToString(stream);
            Log.i("negano", "response is " + response);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String inputstreamToString(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            return builder.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final String path = Environment.getExternalStorageDirectory() + "/1.jpg";
        findViewById(R.id.upload).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ServerCommunication();
                uploadUserPhoto(new File(path));
            }
        });

    }
}
  • 写回答

1条回答 默认 最新

  • duanfuxing2212 2014-05-25 23:12
    关注

    this worked For me :

    package ir.negano.Downloader;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import android.os.Handler;
    
    
    public class Downloader {
    public static final Handler HANDLER = new Handler();
    static Thread               DownloadThread;
    
    
    public static interface OnDownloadMoveListener {
    
        public void onRun(int FileSize, int DownloadedSize, int DownloadedPersent);
    }
    
    
    public static void Download(final String onlineFilePath, final String localFilePath, final OnDownloadMoveListener DownloadListener) {
        DownloadThread = new Thread(new Runnable() {
    
            int DownloadedSize    = 0;
            int DownloadedPersent = 0;
    
    
            @Override
            public void run() {
                try {
                    URL url = new URL(onlineFilePath);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setDoOutput(true);
                    connection.connect();
                    final int FileSize = connection.getContentLength();
    
                    InputStream inputStream = connection.getInputStream();
                    FileOutputStream OutPutStream = new FileOutputStream(localFilePath);
                    byte[] buffer = new byte[8 * 1024];
                    int len = 0;
                    while ((len = inputStream.read(buffer)) > 0) {
                        OutPutStream.write(buffer, 0, len);
                        DownloadedSize += len;
                        DownloadedPersent = (int) (((float) DownloadedSize / FileSize) * 100);
                        if (DownloadListener != null)
                        {
                            HANDLER.post(new Runnable() {
    
                                @Override
                                public void run() {
                                    DownloadListener.onRun(FileSize, DownloadedSize, DownloadedPersent);
                                }
                            });
    
                        }
                        if (DownloadedPersent >= 100)
                        {
                            DownloadThread = null;
                        }
                    }
                    OutPutStream.close();
                }
                catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        DownloadThread.start();
    
    }
    

    }

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

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab