0

Hi i need send file to server i use this code

public void PostFile() {

        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        // httpclient.
        HttpPost httppost = new HttpPost("http://fastcalc.orionsource.ru/test.php");


        File file = cache.getFileFromCache("citys.json");

        FileEntity reqEntity = new FileEntity(file,"text/plain");
        reqEntity.setContentType("text/plain");
        httppost.setEntity(reqEntity);



        System.out.println("executing request " + httppost.getRequestLine());

        HttpResponse response;
        try {
            response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println(EntityUtils.toString(resEntity));
            }
            if (resEntity != null) {
                resEntity.consumeContent();
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpclient.getConnectionManager().shutdown();
    }

On server side code below

<?php
print 'Files:';
print_r($_FILES);
print '<br><br>';
print "Response:\n";
print_r($_REQUEST);
print '<br><br>';

?>

And on Android i get response

05-29 17:26:29.232: I/System.out(26823): executing request POST http://fastcalc.orionsource.ru/test.php HTTP/1.1
05-29 17:26:29.732: I/System.out(26823): HTTP/1.1 200 OK
05-29 17:26:29.732: I/System.out(26823): Files:Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823): )
05-29 17:26:29.732: I/System.out(26823): <br><br>Response:
05-29 17:26:29.732: I/System.out(26823): Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823): )
05-29 17:26:29.732: I/System.out(26823): <br><br>

I cant use MultipartEntity because it's not included in any Android versions. It thirdpart library.

Someone give please link to tutorial which use only FileEntity.

Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74

1 Answers1

0

Hi i have tries with image file to send on server. You can do one thing that convert your file into base64 string and send it to server and from server side try converting base64 to file

for reference how to convert to base64 ses my answer : how to send the image in an ImageView to a php server?

Community
  • 1
  • 1
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • Yes its work, but i need on server side do base64_decode on PHP. I do not need this, because i want use ONE form for HTML POST and Android, HTML post use field to sending file. – Dmitry Nelepov May 29 '12 at 13:52