Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTp post request in android
    primarykey
    data
    text
    <p>I am new to android. I was trying http post request but was getting connection failed . Below is the given code</p> <pre><code>private void postRequest(String givenUsername, String givenPassword) { String paramUsername = givenUsername; String paramPassword = givenPassword; System.out.println("*** doInBackground ** paramUsername " + paramUsername + " paramPassword :" + paramPassword); HttpClient httpClient = new DefaultHttpClient(); // In a POST request, we don't pass the values in the URL. // Therefore we use only the web page URL as the parameter of the HttpPost argument HttpPost httpPost = new HttpPost("url"); httpPost.addHeader("Content-Length", "40"); httpPost.addHeader("Accept", "application/json,text/javascript, */*"); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); // httpPost.s // Because we are not passing values over the URL, we should have a mechanism to pass the // values that can be // uniquely separate by the other end. // To achieve that we use BasicNameValuePair // Things we need to pass with the POST request BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("username", paramUsername); BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("password", paramPassword); // We add the content that we want to pass with the POST request to as name-value pairs // Now we put those sending details to an ArrayList with type safe of NameValuePair List&lt;NameValuePair&gt; nameValuePairList = new ArrayList&lt;NameValuePair&gt;(); nameValuePairList.add(usernameBasicNameValuePair); nameValuePairList.add(passwordBasicNameValuePAir); try { // UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs. // This is typically useful while sending an HTTP POST request. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList); // setEntity() hands the entity (here it is urlEncodedFormEntity) to the request. httpPost.setEntity(urlEncodedFormEntity); try { // HttpResponse is an interface just like HttpPost. // Therefore we can't initialize them HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { Log.i("RESPONSE", EntityUtils.toString(resEntity)); } // According to the JAVA API, InputStream constructor do nothing. // So we can't initialize InputStream although it is not an interface InputStream inputStream = httpResponse.getEntity().getContent(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String bufferedStrChunk = null; while ((bufferedStrChunk = bufferedReader.readLine()) != null) { stringBuilder.append(bufferedStrChunk); } System.out.println("Initial set of cookies:"); List&lt;Cookie&gt; cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i &lt; cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } System.out.println("Post logon cookies:"); cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i &lt; cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } catch (ClientProtocolException cpe) { System.out.println("First Exception caz of HttpResponese :" + cpe); cpe.printStackTrace(); } catch (IOException ioe) { System.out.println("Second Exception caz of HttpResponse :" + ioe); ioe.printStackTrace(); } } catch (UnsupportedEncodingException uee) { System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee); uee.printStackTrace(); } } </code></pre> <p>I am sending username and password for my Login screen .and it will be returning me a cookie. Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload