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("foo1", "test");
jsonObject.accumulate("foo2", "test2");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 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());
}
As per your Edited Question, I think you have to use **[HttpGet][1]** to send your authentication on Server.
[**Here**][2] I found example of HttpGet request in Android.
and also **[Here][3]** found StackOverflow Solution.
##**EDITED:**
I have checked your [**POSTMAN**][4] and found that your server accepting JSON Data.
You can see in Image:
![enter image description here][5]
so now you have to try this code:
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
usernamefromuser = user.getText().toString();
passfromuser = pass.getText().toString();
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("email", usernamefromuser);
jsonObject.accumulate("pass", passfromuser);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 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);
responsecode = httpresponse.getStatusLine().getStatusCode();
responseStr = EntityUtils.toString(httpresponse.getEntity());
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return null;
}
Sorry for late found the solution.
[1]: http://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html
[2]: http://www.tutorialspoint.com/android/android_php_mysql.htm
[3]: https://stackoverflow.com/a/4660576/1318946
[4]: https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en
[5]: http://i.stack.imgur.com/TVtPg.png