0

I would like upload any data (Username, Description,..) and a photo from my Android-App to my Server (PHP-Script).

By searching for this i found a solution: How to take a photo and send to HTTP POST request with Android?

By click on my send button, the upload-process starts and the php-script works but the $_POST -Vars are empty? Can anybody help me? My request-code:

[...]
case R.id.btnSend:
    String strName = tName.getText().toString();
    String strEmail = tEmail.getText().toString();
    String strDatum = tDatum.getText().toString();
    String strBeschreibung = tBeschreibung.getText().toString();        

    nvPairs = new ArrayList<NameValuePair>(2);
    nvPairs.add(new BasicNameValuePair("sender", "AndoidApp"));
    nvPairs.add(new BasicNameValuePair("name", strName));
    nvPairs.add(new BasicNameValuePair("email", strEmail));
    nvPairs.add(new BasicNameValuePair("datum", strDatum));
    nvPairs.add(new BasicNameValuePair("beschreibung", strBeschreibung));
    nvPairs.add(new BasicNameValuePair("bild", bmpUrl));

    new UploadTask().execute();
}
[...]

private class UploadTask extends AsyncTask<Void, Void, List<String>> {
    @Override
    protected List<String> doInBackground(Void... params) {
        String response = "";
        try {
            response = new MultipartServer().postData(new URL(postUrl), nvPairs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }       
}

On the MultipartServer-Code i changed the line:

if ((avatarName = pair.getName()).equals("avatar")) {
    avatarPath = pair.getValue();
}

to this:

if ((avatarName = pair.getName()).equals("bild")) {
    avatarPath = pair.getValue();
}

And the Variables "bmpUrl" and "postUrl" are set correctly at an another line in my code.

11
  • the $_POST -Vars are empty?. That is php. But you did not show your php script. Please do. What is the return string if postData()? Commented Nov 12, 2014 at 9:54
  • In new UploadTask().execute(); you're not passing the data. See the documentation for an example and specifically take note that execute is passed variables when called. Commented Nov 12, 2014 at 9:58
  • @Jeremy. Please don't confuse the OP. He is passing all name value pairs as parameter of the postData() call. Commented Nov 12, 2014 at 10:03
  • @greenapps Sorry for your confusion. Do you see the scope such that those variables are in the async task in a thread-safe fashion? Commented Nov 12, 2014 at 10:06
  • Think so. I've never seen problems AsyncTask's using global (to activity) data. Commented Nov 12, 2014 at 10:11

1 Answer 1

1

Thanks for your help! I found an solution for the problem. The problem was at the Script from How to take a photo and send to HTTP POST request with Android?

I changed it to this:

public class MultipartServer {

    private static final String TAG = "MultipartServer";
    private static final String crlf = "\r\n";
    private static final String twoHyphens = "--";
    private static final String boundary = "AaB03x"; //*****
    private String avatarName = null;
    private String avatarPath = null;

    public String postData(URL url, List<NameValuePair> nameValuePairs) throws IOException {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(15000);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        FileInputStream inputStream;
        OutputStream outputStream = connection.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

        for (NameValuePair pair : nameValuePairs) {
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
            dataOutputStream.writeBytes(
                    "Content-Disposition: form-data; name=\""
                    + URLEncoder.encode(pair.getName(), "UTF-8") 
                    + "\";" + crlf);
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(URLEncoder.encode(pair.getValue(), "UTF-8"));
            if (pair.getName().equals("bild")) {
                avatarName = pair.getName();
                avatarPath = pair.getValue();
            }
        }

        // Write Avatar (if any)
        if (avatarName != null && avatarPath != null) {
            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
            dataOutputStream
                    .writeBytes("Content-Disposition: form-data; name=\""
                            + avatarName + "\";filename=\""
                            + new File(avatarPath).getName() + "\";" + crlf);
            dataOutputStream.writeBytes(crlf);

            inputStream = new FileInputStream(avatarPath);
            byte[] data = new byte[1024];
            int read;
            while ((read = inputStream.read(data)) != -1)
                dataOutputStream.write(data, 0, read);
            inputStream.close();

            dataOutputStream.writeBytes(crlf);
            dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
        }

        dataOutputStream.flush();
        dataOutputStream.close();

        String responseMessage = connection.getResponseMessage();
        Log.d(TAG, responseMessage);

        InputStream in = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(in, "UTF-8"));

        StringBuilder response = new StringBuilder();
        char[] b = new char[512];
        int read;
        while ((read = bufferedReader.read(b)) != -1) {
            response.append(b, 0, read);
        }

        connection.disconnect();
        Log.d(TAG, response.toString());
        return response.toString();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.