0

I have a client who has developed the webservices using GET method, and I'm creating the android app for him. I always used HttpClient/HttpPost to send images to the server, but know i need to send it using HttpGet instead and i'm a bit lost here...

I was searching some example for hours but i found nothing to clarify me. I also tried search for HttpURLConnection using GET but all the examples i found were based on POST.

Could someone give me a hint?

Thanks

lienmt
  • 147
  • 8
  • 1
    you can't send a file using GET. – njzk2 May 06 '14 at 17:13
  • 1
    HTTP method GET is not appropriate for sending images. You should use POST or PUT. – hgoebl May 06 '14 at 17:14
  • yesss @hgoebl, that's why i always used POST but my client want to use GET instead....so, is like njzk2 say and we can't or is just not appropiate? – lienmt May 06 '14 at 17:34
  • We can't. GET requests should be used only to retrieve data. See more at: http://www.w3schools.com/tags/ref_httpmethods.asp – Felipe Vasconcelos May 06 '14 at 17:45
  • 1
    Theoretically you could mis-use GET for small 1x1 pixel images like 'transparent.gif', but for all other images it's not possible. Tell your client that it's not possible. Forget about GET! – hgoebl May 06 '14 at 20:32

1 Answers1

0

You can use this code to post other file types such as an 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", "mike@testmail.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();
    }
  }
}
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • Thank you for your reply, but my doubt is how to send files (such as an image) using HttpGet and not POST such in your code... – lienmt May 06 '14 at 17:32