0

i need to upload a very small sized .xml file to a php script. This script will parse the file, create some stuff on a map, and then the application will display the result.

The problem is that, if i have to upload a file with an html page, i create a form, put an input field with "name=userfile" and then server side i find it in $_FILES['userfile'].

How to do this in android? with string is very simple with

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add("key", "value");

but now?

this is my code client side but no file seems to hit the server:

final String url = "http://www.blablabladasdasd/mysite/test.php";
    String selected = getIntent().getStringExtra("com.example.ghghgh.filename");
    final File file = new File("/storage/sdcard0/myFolder/"+selected + ".xml");
    Log.i("FILE SIZE: ", "" + file.length());

    final WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.setWebViewClient(new MyWebViewClient());



    Thread thread = new Thread()
    {
        public void run()
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            client.getParams().setParameter("userfile", file);

            try
            {
                HttpResponse response = client.execute(post);
                String res = EntityUtils.toString(response.getEntity());
                Log.i("RESPONSE: ", res);
                mWebView.loadData(res, "text/html", HTTP.UTF_8);

            }
            catch(ClientProtocolException e)
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    };
    thread.start();

    try
    {
        thread.join();
    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }
Fujitina
  • 121
  • 1
  • 2
  • 15

1 Answers1

0

I had to solve a similar issue, and found my answers right here: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/ Basically, life gets easier with a few extra Apache libraries. As a bonus, here is how to do the same thing, but over a secure (SSL) connection: Secure HTTP Post in Android

You might be misinterpreting the docs for loadData, as it expects 'base64' or URL encoded data, otherwise. Not XML or HTML. Perhaps loadURL works better?

Hope this helps

Community
  • 1
  • 1
Sandor
  • 31
  • 3
  • I come from something similar, and i had problem with deprecated object, btw i will make another try with your link thank you – Fujitina Oct 03 '13 at 17:58
  • mmh something is happen, i have the entire response html code in the string "res" but when i try "loadData(res, "text/html", HTTP.UTF_8)" it doesn't display the page – Fujitina Oct 03 '13 at 18:52