2014-05-06 2 views
0

J'ai un client qui a développé les webservices en utilisant la méthode GET, et je crée l'application Android pour lui. J'ai toujours utilisé HttpClient/HttpPost pour envoyer des images au serveur, mais sachez que je dois l'envoyer en utilisant HttpGet à la place et je suis un peu perdu ici ...Comment puis-je utiliser HttpClient/HttpGet pour envoyer un fichier au serveur?

Je cherchais un exemple pendant des heures mais je n'ai rien trouvé à clarifie-moi. J'ai également essayé de chercher HttpURLConnection en utilisant GET mais tous les exemples que j'ai trouvés étaient basés sur POST.

Quelqu'un pourrait-il me donner un indice?

Merci

+1

vous ne pouvez pas envoyer de fichier à l'aide de GET. – njzk2

+1

La méthode HTTP GET n'est pas appropriée pour l'envoi d'images. Vous devriez utiliser POST ou PUT. – hgoebl

+0

yesss @hgoebl, c'est pourquoi j'ai toujours utilisé POST mais mon client veut utiliser GET à la place .... alors, est comme njzk2 dire et nous ne pouvons pas ou n'est tout simplement pas approprié? – lienmt

Répondre

0

Vous pouvez utiliser ce code pour afficher d'autres types de fichiers comme une image.

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.HttpMultipartMode; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.app.Activity; 

public class MainActivity extends Activity { 

private static final String TAG = "MainActivity.java"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // we are going to use asynctask to prevent network on main thread exception 
    new PostDataAsyncTask().execute(); 

} 

public class PostDataAsyncTask extends AsyncTask<String, String, String> { 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     // do stuff before posting data 
    } 

    @Override 
    protected String doInBackground(String... strings) { 
     try { 

      // 1 = post text data, 2 = post file 
      int actionChoice = 2; 

      // post a text data 
      if(actionChoice==1){ 
       postText(); 
      } 

      // post a file 
      else{ 
       postFile(); 
      } 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String lenghtOfFile) { 
     // do stuff after posting data 
    } 
} 

// this will post our text data 
private void postText(){ 
    try{ 
     // url where the data will be posted 
     String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php"; 
     Log.v(TAG, "postURL: " + postReceiverUrl); 

     // HttpClient 
     HttpClient httpClient = new DefaultHttpClient(); 

     // post header 
     HttpPost httpPost = new HttpPost(postReceiverUrl); 

     // add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("firstname", "Mike")); 
     nameValuePairs.add(new BasicNameValuePair("lastname", "Dalisay")); 
     nameValuePairs.add(new BasicNameValuePair("email", "[email protected]com")); 

     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // execute HTTP post request 
     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v(TAG, "Response: " + responseStr); 

      // you can add an if statement here and do other actions based on the response 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

// will post our text file 
private void postFile(){ 
    try{ 

     // the file to be posted 
     String textFile = Environment.getExternalStorageDirectory() + "/sample.txt"; 
     Log.v(TAG, "textFile: " + textFile); 

     // the URL where the file will be posted 
     String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php"; 
     Log.v(TAG, "postURL: " + postReceiverUrl); 

     // new HttpClient 
     HttpClient httpClient = new DefaultHttpClient(); 

     // post header 
     HttpPost httpPost = new HttpPost(postReceiverUrl); 

     File file = new File(textFile); 
     FileBody fileBody = new FileBody(file); 

     MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("file", fileBody); 
     httpPost.setEntity(reqEntity); 

     // execute HTTP post request 
     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v(TAG, "Response: " + responseStr); 

      // you can add an if statement here and do other actions based on the response 
     } 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

Merci pour votre réponse, mais mon doute est de savoir comment envoyer des fichiers (comme une image) en utilisant HttpGet et non POST tel dans votre code ... – lienmt

Questions connexes