Last active
August 29, 2015 14:10
-
-
Save devrath/4f1de3e56c69a81e3c93 to your computer and use it in GitHub Desktop.
Http post request with no Encoding and reading the stream revieved from the server
This file contains 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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
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.StringEntity; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.json.JSONObject; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
TextView edt; | |
String url="http://YOUR-URL/samplepost"; | |
String finalResult=""; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
edt=(TextView) findViewById(R.id.txtId); | |
new LongOperation().execute(""); | |
} | |
private class LongOperation extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
finalResult=postData(); | |
return "Executed"; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
Log.d("ANSWER", finalResult); | |
edt.setText(finalResult); | |
} | |
@Override | |
protected void onPreExecute() {} | |
@Override | |
protected void onProgressUpdate(Void... values) {} | |
} | |
public String postData() { | |
InputStream inputStream = null; | |
String result = ""; | |
try { | |
// 1. create HttpClient | |
HttpClient httpclient = new DefaultHttpClient(); | |
// 2. make POST request to the given URL | |
HttpPost httpPost = new HttpPost(url); | |
String json = ""; | |
// 3. build jsonObject | |
JSONObject jsonObject = new JSONObject(); | |
jsonObject.accumulate("name", "carl"); | |
//SET THE HTTP PARAMS | |
HttpParams httpParameters = httpPost.getParams(); | |
// Set the connection timeout in milliseconds until a connection is established. | |
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); | |
// Set the socket timeout in milliseconds which is the timeout for waiting for data. | |
HttpConnectionParams.setSoTimeout(httpParameters, 10000); | |
// 4. convert JSONObject to JSON to String | |
json = jsonObject.toString(); | |
// 5. set json to StringEntity | |
StringEntity se = new StringEntity(json); | |
// 6. set httpPost Entity | |
httpPost.setEntity(se); | |
// 7. Set some headers to inform server about the type of the content | |
httpPost.setHeader("Accept", "application/json"); | |
httpPost.setHeader("Content-type", "application/json"); | |
// 8. Execute POST request to the given URL | |
HttpResponse httpResponse = httpclient.execute(httpPost); | |
// 9. receive response as inputStream | |
inputStream = httpResponse.getEntity().getContent(); | |
// 10. convert inputstream to string | |
if(inputStream != null) | |
result = convertInputStreamToString(inputStream); | |
else | |
result = "Did not work!"; | |
} catch (Exception e) { | |
Log.d("InputStream", e.getLocalizedMessage()); | |
} | |
// 11. return result | |
return result; | |
} | |
private static String convertInputStreamToString(InputStream inputStream) throws IOException{ | |
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); | |
String line = ""; | |
String result = ""; | |
while((line = bufferedReader.readLine()) != null) | |
result += line; | |
inputStream.close(); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment