2011-02-28 2 views
0

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)

+0

Que voulez-vous dire par "forme PHP"? Un formulaire HTML qui envoie des données à une application PHP? –

+0

Oui J'ai une application PHP qui traite les données saisies à partir d'un formulaire HTML – nmock

Répondre

0

Jetez un coup d'œil à this snippet.

Cependant, je ne sais pas comment envoyer des données FICHIER.

Bonne chance
Tom

2

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) { 
    } 

} 
+0

Pouvez-vous élaborer sur la conversion en $ _FILE? Ce fut la partie que j'ai été particulièrement confus, en envoyant tous les deux à l'application php – nmock

+0

@nmock: Désolé pour AFK, mais @fd a répondu très bien à une autre réponse dans ce fil. – Wroclai

0

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.

http://hc.apache.org/httpcomponents-client-ga/apidocs/org/apache/http/entity/mime/MultipartEntity.html

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); 
0

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(); 
     } 
Questions connexes