4

In my last app I'm going to use Koush Ion library. It's so handy but i have a problem with uploading file to my rest server. note: My server response to success upload Process is 1

My code i like this:

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

                                }
                            })
                            ;
            }
        });

But I got TimeoutException from server. my questions are: 1. Is it right way for selecting file that I've done?! 2. Should is use Future Callback as String?!

posting data to web serveer with webform

I check my request to server by fiddler2, when I try to upload file to server...it show me that request send, multipartParameters send but when try to send file...fiddler show me the error:

Protocol Violation Report:
Content-Length mismatch: Request Header indicated 455 bytes, but client sent 387 bytes.
MAY3AM
  • 1,182
  • 3
  • 17
  • 42
  • What version of ion? What is your server backend? Do other http calls to the server with ion work? – koush May 15 '14 at 01:53
  • @koush the latest one, and yeah, I can make http call ( i can send json object and get json object as result ).....please help me. Thank you very much :) – MAY3AM May 15 '14 at 04:09
  • Hey, Did you solve this issue ? I'm having the same thing – Danpe Sep 18 '14 at 21:30
  • @Danpe No man, this is an open issue https://github.com/koush/ion/issues/362 – MAY3AM Sep 19 '14 at 07:40

2 Answers2

7

I actually works for me, Here is my code:

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

Hope it helps someone :)

Danpe
  • 18,668
  • 21
  • 96
  • 131
  • Thanks man, Now I'm using Loopj Library for this part of my app, I'll use of your solution after first release. – MAY3AM Oct 02 '14 at 07:28
  • In Urls.UPLOAD_PICTURE are you using a script on a server or is it pointing to a folder on the server? – ymerdrengene Aug 17 '16 at 14:47
3

Searched pretty long for this and noticed that the important thing was to include POST in .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)
            {

            }
        });
mmackh
  • 3,550
  • 3
  • 35
  • 51