android file-upload android-asynctask ion-koush

android - Carga de archivos de varias partes con la biblioteca Koush Ion



file-upload android-asynctask (2)

Busqué bastante tiempo para esto y noté que lo importante era incluir POST en .load ()

Ion.with(getBaseContext()).load("POST",url).uploadProgressHandler(new ProgressCallback() { @Override public void onProgress(long uploaded, long total) { System.out.println("uploaded " + (int)uploaded + " Total: "+total); } }).setMultipartParameter("platform", "android").setMultipartFile("image", new File(getPath(selectedImage))).asString().setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String result) { } });

En mi última aplicación, voy a usar la biblioteca Koush Ion . Es muy útil, pero tengo un problema con la carga de archivos a mi servidor de descanso. nota: la respuesta de mi servidor al proceso de carga exitosa es 1

Mi código me gusta esto:

public class MainActivity extends Activity { Button upload, login; TextView uploadCount; ProgressBar progressBar; String token, FilePath; Future<String> uploading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); upload = (Button) findViewById(R.id.upload); uploadCount = (TextView) findViewById(R.id.upload_count); progressBar = (ProgressBar) findViewById(R.id.progress); token = "147c85ce29dc585966271280d59899a02b94c020"; FilePath = Environment.getExternalStorageDirectory().toString()+"/police.mp3"; upload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (uploading !=null && !uploading.isCancelled()){ resetUpload(); return; } upload.setText("Uploading..."); uploading = Ion.with(MainActivity.this) .load("http://myserver.com/api/v1/tone/upload/?token="+token) .setLogging("UPLOAD LOGS:", Log.DEBUG) .uploadProgressBar(progressBar) .uploadProgressHandler(new ProgressCallback() { @Override public void onProgress(int downloaded, int total) { uploadCount.setText("" + downloaded + "/" + total); } }) .setMultipartParameter("title", "police") .setMultipartParameter("category", "7") .setMultipartFile("file_url", new File(FilePath)) .asString() .setCallback( new FutureCallback<String>() { @Override public void onCompleted(Exception e, String result) { // TODO Auto-generated method stub } }) ; } });

Pero tengo TimeoutException del servidor. mis preguntas son: 1. ¿Es la forma correcta de seleccionar el archivo que he hecho? 2. ¿Debería usar Future Callback como String?

Verifico mi solicitud al servidor por fiddler2, cuando trato de subir el archivo al servidor ... me muestra esa solicitud de envío, multipartParameters envía pero cuando intento enviar un archivo ... violín muéstrame el error:

Protocol Violation Report: Content-Length mismatch: Request Header indicated 455 bytes, but client sent 387 bytes.


De hecho, yo trabajo para mí, aquí está mi código:

final File fileToUpload = new File(localFilePath); Ion.with(context) .load(Urls.UPLOAD_PICTURE) .uploadProgressHandler(new ProgressCallback() { @Override public void onProgress(long uploaded, long total) { // Displays the progress bar for the first time. mNotifyManager.notify(notificationId, mBuilder.build()); mBuilder.setProgress((int) total, (int) uploaded, false); } }) .setTimeout(60 * 60 * 1000) .setMultipartFile("upload", "image/jpeg", fileToUpload) .asJsonObject() // run a callback on completion .setCallback(new FutureCallback<JsonObject>() { @Override public void onCompleted(Exception e, JsonObject result) { // When the loop is finished, updates the notification mBuilder.setContentText("Upload complete") // Removes the progress bar .setProgress(0, 0, false); mNotifyManager.notify(notificationId, mBuilder.build()); if (e != null) { Toast.makeText(context, "Error uploading file", Toast.LENGTH_LONG).show(); return; } Toast.makeText(context, "File upload complete", Toast.LENGTH_LONG).show(); } }); }

Espero que ayude a alguien :)