2014-05-09 8 views
6

io uso apache-mime4j-0.6.jar e httpmime-4.0.1.jar, nel mio Logcat Tutto è buono tranne questo Registro out_write() limiting sleep time 31178 to 23219 con tag audio_hw_primary ma non sono sicuro che sia Dalla mia App.app Android post TUTTO tranne file inviato

Problema: nel mio file php, posso ricevere tutto tranne il file inviato!

questo è il mio codice, base su this Domanda

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

    } 
} 

risposta

3

questo ha lavorato per 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(); 

} 

}

Problemi correlati