How can I send my username and password to the server and get response from the server using httpclient in Android? I researched many websites but I couldn't find anything I want.
Asked
Active
Viewed 1.1k times
2
-
2Your question isn't unclear. What username and password? What server? What response are you expecting? – La bla bla Jan 19 '13 at 11:21
-
You have to make **web service** on the server-side. Then there are at least 2 ways to send the data to the server and get the response from your code. Search for `HttpPost` & `HttpUrlConnection`. The first is easier to use. – Mickäel A. Jan 19 '13 at 11:33
3 Answers
9
Make a class Adapter and put this code...Here i assume you are using json webService.
public String login(String uname, String password)
{
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("Your Url");
nameValuePairs.add(new BasicNameValuePair("emailid", uname));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line).append("\n");
}
is.close();
result = sb.toString();
Log.v("log","Result: " + result);
}
catch (Exception e)
{
Log.v("log", "Error converting result " + e.toString());
}
return result;
}
and make a call like this...
Adapter adb = new Adapter();
response = adb.login(edtLoginemail.getText().toString(), edtLoginPassword.getText().toString());

ahall
- 129
- 2
- 7

Mehul Ranpara
- 4,245
- 2
- 26
- 39
-
@mehul-ranpara how can i send `emailid` and `password` using array.eg.. `user_auth` – hash Nov 20 '14 at 11:09
-
1@Sameera you can pass array to this class and from that you can take value for ex :L arr.get(0)-- user_auth or arr.get(1)-- user_password etc. – Mehul Ranpara Nov 21 '14 at 08:47
-
@mehul-ranpara can you please help me in here http://stackoverflow.com/questions/27053916/how-to-send-json-data-from-android-to-php-url/27054118?noredirect=1#comment42626602_27054118 – hash Nov 21 '14 at 08:56
0
Try this it might help you.
userId = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<login><User>" + userId + "</User>"
+ "<Password>" + password + "</Password></login>";
serverCall(xml);
serverCall(xml);
private void serverCall(final String xml )
{
ConnectivityManager conMan = ( ConnectivityManager ) getSystemService( Context.CONNECTIVITY_SERVICE );
if( conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected() )
{
result = new Connection().getResponse("http://localhost/login.php" , xml);
if(result.equals("yes") {
//registration done
} else {
//failed
}
}
}
Connection.java
public String getResponse(String connUrl, String xml) {
DefaultHttpClient httpClient = null;
try {
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpPost method = new HttpPost(connUrl);
method.setEntity(mp);
mp.addPart("xml_request", new StringBody(xml));
httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(method);
if(response.getStatusLine().getStatusCode() == 200){
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
return outstream.toString();
}
}
catch(Exception e) {
Log.v("APP", "Exception while create connection for getResponse from server with: " + e.getMessage());
}
finally {
httpClient.getConnectionManager().shutdown();
}
return "";
}

Ajay S
- 48,003
- 27
- 91
- 111
0
You have to make a web service on the server-side.
Then there are at least 2 ways to send the data to the server and get the response from your code. Search for HttpPost
& HttpUrlConnection
. The first is easier to use to send data with the method POST, but is also a LOT more slow.

Mickäel A.
- 9,012
- 5
- 54
- 71