Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app post everyThing except posted file

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

    }
}
like image 818
Mojtaba Yeganeh Avatar asked May 09 '14 14:05

Mojtaba Yeganeh


1 Answers

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

}

}

like image 56
Mojtaba Yeganeh Avatar answered Oct 25 '22 11:10

Mojtaba Yeganeh