Created
March 10, 2014 15:45
-
-
Save qwo/9467500 to your computer and use it in GitHub Desktop.
Async Post requests in Android. Best Solution I've found. See top for usage info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//great for async tasks in android! | |
// FROM http://stackoverflow.com/questions/7860538/android-http-post-asynctask | |
/* | |
HashMap<String, String> data = new HashMap<String, String>(); | |
data.put("key1", "value1"); | |
data.put("key2", "value2"); | |
AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data); | |
asyncHttpPost.execute("http://example.com"); | |
*/ | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.StatusLine; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.util.EntityUtils; | |
import android.os.AsyncTask; | |
public class AsyncHttpPost extends AsyncTask<String, String, String> { | |
private HashMap<String, String> mData = null;// post data | |
/** | |
* constructor | |
*/ | |
public AsyncHttpPost(HashMap<String, String> data) { | |
mData = data; | |
} | |
/** | |
* background | |
*/ | |
@Override | |
protected String doInBackground(String... params) { | |
byte[] result = null; | |
String str = ""; | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL | |
try { | |
// set up post data | |
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); | |
Iterator<String> it = mData.keySet().iterator(); | |
while (it.hasNext()) { | |
String key = it.next(); | |
nameValuePair.add(new BasicNameValuePair(key, mData.get(key))); | |
} | |
post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8")); | |
HttpResponse response = client.execute(post); | |
StatusLine statusLine = response.getStatusLine(); | |
if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){ | |
result = EntityUtils.toByteArray(response.getEntity()); | |
str = new String(result, "UTF-8"); | |
} | |
} | |
catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
catch (Exception e) { | |
} | |
return str; | |
} | |
/** | |
* on getting result | |
*/ | |
@Override | |
protected void onPostExecute(String result) { | |
// something... | |
} | |
} |
Thanks but "DefaultHttpClient" is deprecated .....
What is alternative method for such deprecated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice explaintion