0

I need to modify my java Class to allow me to successfully upload a file to the URL within.
I want to use the web browser's built-in file upload mechanism.

This is what I have done so far:-

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web=(WebView)findViewById(R.id.web);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://encodable.com/uploaddemo/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • What do mean upload via native Java? You want to POST/PUT to a http URL? Why not do that via Java, not the browser. The browser has all sorts of protections around the upload feature, for security reasons. – Ted Johnson Feb 02 '14 at 14:56
  • 2
    StackOverflow is for programming questions, not demands. You are welcome to hire somebody to whom you can bark orders like this. Otherwise, please edit this post to ask specific questions about the problems you are encountering. – CommonsWare Feb 02 '14 at 14:57
  • @CommonsWare Edited, sorry for my bad language. – user3259926 Feb 02 '14 at 15:04
  • @TedJohnson I meant plain java, not using android API.. edited my question. – user3259926 Feb 02 '14 at 15:06
  • I appreciate the improvement in tone. However, this still does not really ask a question. I am assuming that your question is "how do I write this?". I suppose that if you could figure out some JavaScript that can automate this, you could use `loadUrl("javascript:...")` (or `evaluateJavascript()` on API Level 19+) from your Java code. – CommonsWare Feb 02 '14 at 15:11
  • @CommonsWare So the only way is using a javascript? isn't there any inbuild mechanism in android for this? – user3259926 Feb 02 '14 at 15:16
  • "isn't there any inbuild mechanism in android for this?" -- no. `WebView` does not expose much of anything that depends upon the specific HTML, CSS, and JavaScript that is loaded. For example, there is no way for you to fill in a form field, submit a form, or otherwise modify the DOM from Java, except by way of evaluating some JavaScript in the context of the loaded page (per my previous comment). – CommonsWare Feb 02 '14 at 15:20

2 Answers2

2

Use this class to upload your file...hope this might be helpful to you

public class TryFile {
    public static void main(String[] ar)
           throws HttpException, IOException, URISyntaxException {
        TryFile t = new TryFile();
        t.method();
    }
    public void method() throws HttpException, IOException, URISyntaxException {
        String url = "http://encodable.com/uploaddemo/";
        String fileName = ""; //file name to be uploaded
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        FileBody fileContent = new FiSystem.out.println("hello");
        StringBody comment = new StringBody("Filename: " + fileName);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", fileContent);
        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
    }
}
user2226755
  • 12,494
  • 5
  • 50
  • 73
ajitksharma
  • 4,523
  • 2
  • 21
  • 40
1

I believe what you're going to do is not what Android platform expects from you anyhow. You have to upload your stuff using Android API. You can find some useful information in this thread: Android: upload file with filling out POST body together or other similar ones.

Community
  • 1
  • 1
Aleks N.
  • 6,051
  • 3
  • 42
  • 42