4

After reading a lot of Questions and answers, I've decided to use MultipartEntity. This Answer must be the best: https://stackoverflow.com/a/2937140/1388723

I've implemented it:

private class SendImage extends AsyncTask<String, Void, Void> {
    String sresponse;

    @Override
    protected Void doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(
                "http://taras.xin.com.pl/loadimage.php");
        InputStream in = null;
        try {
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            Log.i("MultyPart", params[0]);
            File file = new File(params[0]);
            Log.i("MultyPartFILE", file.toString());
            ContentBody fb = new FileBody(file, "image/jpeg");
            Log.i("MultyPartFB", fb.toString());
            entity.addPart("text", new StringBody(
                    "This text was added to posrt"));
            entity.addPart("word", new StringBody(
                    "This text was added to post"));
            entity.addPart("file", fb);
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            in = response.getEntity().getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            in.close();
            sresponse = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                text.setText(sresponse);
            }
        });

        super.onPostExecute(result);
    }
}

and my loadimage.php:

    <?php
     var_dump($_POST);
     var_dump($_FILE);
      ?>

I receive two strings to $_POST, but there is no even shadow of FileBody(file). $_FILE gives null.

To be exact I've downloaded httpcomponents-client-4.2.2-bin, extracted httpmime-4.2.2.jar.

Community
  • 1
  • 1
BAZTED
  • 546
  • 5
  • 16

1 Answers1

0

try to get only the File you are sending
$file = $_FILES['file'];

user2033349
  • 175
  • 1
  • 2
  • 15