Post requests received as GET with android HttpURL

2019-07-25 09:41发布

问题:

I am trying to upload a binary file (a serialized object) to my Google app engine server, but I only receive GET requests. Here's my code:

private void sendPostRequest() {
if (!isConnected()) {
    return;
}

File file = new File(this._context.getFilesDir() + "/" + FILE_NAME);
FileInputStream fis;
try {
    fis = new FileInputStream(file);
    int length = fis.available();
    URL url = new URL(URL);
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    String urlParameters = URLEncoder.encode("family", "UTF-8") + "="
            + URLEncoder.encode("ohayon", "UTF-8");
    byte[] buffer = new byte[length];
    urlParameters += "&data=" + fis.read(buffer);
    connection.setRequestProperty("Content-Length",
            "" + Integer.toString(urlParameters.getBytes().length));
    connection.setUseCaches(false);

    // BufferedWriter we = new BufferedWriter(new OutputStreamWriter(
    // connection.getOutputStream(), "UTF-8"));
    DataOutputStream wr = new DataOutputStream(
            connection.getOutputStream());
    wr.write(urlParameters.getBytes());
    wr.flush();
    wr.close();
    connection.disconnect();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

Any idea what I'm doing wrong? Also comments about implementation would be appreciated as this is my first attempt at networking with android

Thanks!