Here, the post will explain about the web service using POST method in ANDROID.
1. Please find the link about POST method in Web API http://docs.oracle.com/cd/E19798-01/821-1841/giepu/index.html.
2. HTTP POST allows you to send data to the server in different formats such as XML, JSON or binary.
The following code will explain about the LOGIN API using POST. Here, we can pass our USERNAME and PASSWORD with the corresponding web server URL. Once we have posted we will be getting the response from the corresponding web server.
--------------------------------------------------------------------------------------------
Request:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.clear();
params.add(new BasicNameValuePair(“UserName”,”YOUR NAME”));
params.add(new BasicNameValuePair(“UserPwd”, “YOUR PASSWORD”));
params.clear();
params.add(new BasicNameValuePair(“UserName”,”YOUR NAME”));
params.add(new BasicNameValuePair(“UserPwd”, “YOUR PASSWORD”));
new CallLoginAPI().execute();
Here, the Class CallLoginAPI() is used to perform the task inside AsyncTask. So it will not freeze the UI.
public class CallLoginAPI extends AsyncTask<Void, Void, Integer> {
@Override
protected void onPreExecute() {// TODO Auto-generated method stub
progressDialog = ProgressDialog.show(LoginActivity.this, “Logging in”, “Please Wait”);
}
@Override
protected Integer doInBackground(Void… arg0) {
// TODO Auto-generated method stub
try {
CallWebURL();
} catch (Exception e) { }
return 0;
}
@SuppressLint(“NewApi”)
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
try
{
progressDialog.dismiss();
{
progressDialog.dismiss();
if (result == 0) {
System.out.println(“RESPONSE:” + RESPONSE);
if (RESPONSE != null && RESPONSE.equalsIgnoreCase(“CONNECTION SUCCESS”)) {
Toast.makeText(LoginActivity.this, ” Login Success “, Toast.LENGTH_LONG).show(); }
else
{
Toast.makeText(LoginActivity.this,” Invalid UserName and Password “, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println(“RESPONSE:” + RESPONSE);
if (RESPONSE != null && RESPONSE.equalsIgnoreCase(“CONNECTION SUCCESS”)) {
Toast.makeText(LoginActivity.this, ” Login Success “, Toast.LENGTH_LONG).show(); }
else
{
Toast.makeText(LoginActivity.this,” Invalid UserName and Password “, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
System.out.println(e);
}
}
No comments:
Post a Comment