Ich möchte eine mehrteilige Anfrage mit den folgenden Parametern erstellen: Name (Zeichenfolge), E-Mail (Zeichenfolge) und Dateiupload (Datei). Ich verwende den folgenden Java-Code (funktioniert in Android).Hilfe beim Erstellen einer POST-Anfrage mit MultipartEntity (Newbie Frage)
Die httppost.getRequestLine() druckt
POST http://www.myurl.com/upload HTTP/1.1
So sieht alles gut aus auf der Client-Seite, aber mein Server (Django/Apache) liest es als GET-Anfrage, ohne GET-Parameter - request.method
produziert ' GET ', request.GET.items()
erzeugt ein leeres Wörterbuch.
Was mache ich falsch? Ich weiß nicht wirklich, wie man die mehrteiligen Parameter richtig einstellt - benutze Vermutungen - so ist es möglich, dass das das Problem ist.
public void SendMultipartFile() {
Log.e(LOG_TAG, "SendMultipartFile");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/upload");
File file = new File(Environment.getExternalStorageDirectory(),
"video.3gp");
Log.e(LOG_TAG, "setting up multipart entity");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileupload", cbFile);
Log.i("SendLargeFile", "file length = " + file.length());
try {
mpEntity.addPart("name", new StringBody(name));
mpEntity.addPart("email", new StringBody(email));;
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
Log.e(LOG_TAG, "UnsupportedEncodingException");
e1.printStackTrace();
}
httppost.setEntity(mpEntity);
Log.e(LOG_TAG, "executing request " + httppost.getRequestLine());
HttpResponse response;
try {
Log.e(LOG_TAG, "about to execute");
response = httpclient.execute(httppost);
Log.e(LOG_TAG, "executed");
HttpEntity resEntity = response.getEntity();
Log.e(LOG_TAG, response.getStatusLine().toString());
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();
}
}
Bitte nehmen Sie sich einen Blick auf diese zuvor beantworteten Frage http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk – Omie