【问题标题】:Android app post everyThing except posted fileAndroid应用程序发布除发布文件外的所有内容
【发布时间】:2014-06-27 07:28:08
【问题描述】:

我在我的 Logcat 中使用 apache-mime4j-0.6.jarhttpmime-4.0.1.jar 一切都很好,除了这个带有标签 out_write() limiting sleep time 31178 to 23219 的日志 audio_hw_primary 但我不确定它来自我的应用程序。

问题:在我的 php 文件中,我可以接收除我发布的文件之外的所有内容!

这是我的代码,基于this 问题

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

    }
}

【问题讨论】:

    标签: java php android file-upload


    【解决方案1】:

    这对我有用:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      相关资源
      最近更新 更多