Actuellement, j'ai un formulaire PHP qui accepte les données POST ainsi qu'un fichier ($ _POST/$ _FILE).Envoi de données POST et FILE à partir de Java
Comment utiliser ce formulaire dans Java? (Application Android)
Actuellement, j'ai un formulaire PHP qui accepte les données POST ainsi qu'un fichier ($ _POST/$ _FILE).Envoi de données POST et FILE à partir de Java
Comment utiliser ce formulaire dans Java? (Application Android)
Jetez un coup d'œil à this snippet.
Cependant, je ne sais pas comment envoyer des données FICHIER.
Bonne chance
Tom
Voilà comment vous pouvez envoyer $_POST
par Java (en particulier dans Android). Il ne devrait pas être trop difficile à convertir en $_FILE
. Tout d'ici est un bonus.
public void sendPostData(String url, String text) {
// Setup a HTTP client, HttpPost (that contains data you wanna send) and
// a HttpResponse that gonna catch a response.
DefaultHttpClient postClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
try {
// Make a List. Increase the size as you wish.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// Add your form name and a text that belongs to the actual form.
nameValuePairs.add(new BasicNameValuePair("your_form_name", text));
// Set the entity of your HttpPost.
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute your request against the given url and catch the response.
response = postClient.execute(httpPost);
// Status code 200 == successfully posted data.
if(response.getStatusLine().getStatusCode() == 200) {
// Do something. Maybe you wanna get your response
// and see what it contains, with HttpEntity class?
}
} catch (Exception e) {
}
}
On dirait que vous avez besoin de la magie d'un org.apache.http.entity.mime.MultipartEntity
puisque vous mélangez des champs de formulaire avec des champs de fichiers.
File fileObject = ...;
MultiPartEntity entity = new MultiPartEntity();
entity.addPart("exampleField", new StringBody("exampleValue")); // probably need to URL encode Strings
entity.addPart("exampleFile", new FileBody(fileObject));
httpPost.setEntity(entity);
Téléchargez et comprennent l'Apache httpmime-4.0.1.jar et apache-mime4j-0.6.jar. Après cela, envoyer des fichiers via la demande de publication est assez facile.
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.to.your/html-form.php");
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(new File("/sdcard/my_file_to_upload.jpg")));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
Log.e(this.getClass().getSimpleName(), response.toString());
} catch (IOException e) {
e.printStackTrace();
}
Que voulez-vous dire par "forme PHP"? Un formulaire HTML qui envoie des données à une application PHP? –
Oui J'ai une application PHP qui traite les données saisies à partir d'un formulaire HTML – nmock