I am trying to upload a binary file (serialized object) to a Google engine server, but I only get requests GET. Here is 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);
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? Comments on the implementation will also be appreciated, as this is my first attempt to connect to android
Thanks!
Yotam source
share