Example for Restful web service authentication

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Example for Restful web service authentication

Postby dray » Wed Feb 16, 2011 11:11 am

Hi,
anyone help me how to POST authentication details to a restful web service and to get response from it.
I have to post Username, IsAuthenticated(ie. true or false), Password.Also explain the url encoding method too.
I have shown my code below.I am a Beginner in Android.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LoginActivity extends Activity
  2. {
  3.         String Username;
  4.         String Password;
  5.         String IsAuthenticated;
  6.         String answer;
  7.        
  8.         @Override
  9.          public void onCreate(Bundle savedInstanceState) {
  10.                super.onCreate(savedInstanceState);
  11.                setContentView(R.layout.main);
  12.                         try {
  13.                                 POST(Username,Password,IsAuthenticated);
  14.                         } catch (Exception e) {
  15.                                 e.printStackTrace();
  16.                         }
  17.           }
  18.  
  19.          
  20.          public String POST(String Username, String IsAuthenticated, String Password) {
  21.                  String Returned = null;
  22.               HttpClient httpclient = new DefaultHttpClient();
  23.               HttpPost httppost = new HttpPost("http://.................../Authenticate");
  24.              
  25.               try {
  26.                  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  27.                  // Your DATA
  28.                  nameValuePairs.add(new BasicNameValuePair("UserName", "username"));
  29.                  nameValuePairs.add(new BasicNameValuePair("IsAuthenticated", "false"));
  30.                  nameValuePairs.add(new BasicNameValuePair("Password", "password"));
  31.  
  32.                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
  33.                  HttpResponse response = httpclient.execute(httppost);
  34.                  HttpEntity resEntity = response.getEntity();
  35.                  Returned = EntityUtils.toString(resEntity);
  36.                
  37.                 System.out.println(Returned);
  38.                  Toast.makeText(this, Returned, Toast.LENGTH_LONG).show();
  39.               } catch (ClientProtocolException e) {
  40.                  Toast.makeText(this, "There was an issue Try again later", Toast.LENGTH_LONG).show();
  41.               } catch (IOException e) {
  42.                  Toast.makeText(this, "There was an IO issue Try again later", Toast.LENGTH_LONG).show();
  43.                  e.printStackTrace();
  44.               }
  45.              
  46.               return Returned;
  47.            }
  48. }
Parsed in 0.056 seconds, using GeSHi 1.0.8.4



Also I have got this error message.
error.tif
My Error Msg
error.tif (114.48 KiB) Viewed 4431 times

Thanks
DRAY
dray
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 03, 2011 12:15 pm

Top

Re: Example for Restful web service authentication

Postby dray » Mon Feb 21, 2011 5:48 am

Hi all.
Finally I have successfully got the response from the web service.
I have posted my code below.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package login.sample;
  2. import java.io.IOException;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.entity.StringEntity;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.message.BasicHeader;
  10. import org.apache.http.protocol.HTTP;
  11. import org.apache.http.util.EntityUtils;
  12. import android.app.Activity;
  13. import android.os.Bundle;
  14. import android.widget.Toast;
  15.  
  16. public class LoginActivity extends Activity
  17. {
  18.         String Returned;
  19.         @Override
  20.          public void onCreate(Bundle savedInstanceState) {
  21.                super.onCreate(savedInstanceState);
  22.                setContentView(R.layout.main);
  23.                
  24.               try {
  25.                   HttpClient httpclient = new DefaultHttpClient();
  26.                   HttpPost post = new HttpPost("http://Your url here/");
  27.                   StringEntity str = new StringEntity("Your xml code");
  28.                   str.setContentType("application/xml; charset=utf-8");
  29.                   str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/xml; charset=utf-8"));
  30.                   post.setEntity(str);
  31.                   HttpResponse response = httpclient.execute(post);
  32.                   HttpEntity entity = response.getEntity();
  33.                   Returned = EntityUtils.toString(entity);
  34.                   Toast.makeText(this, Returned, Toast.LENGTH_LONG).show();
  35.                 } catch ( IOException ioe ) {
  36.                  ioe.printStackTrace();
  37.                 }
  38.               }
  39. }
  40.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

Hope this will helps you.


-DRAY-
Last edited by dray on Wed Sep 12, 2012 6:20 am, edited 1 time in total.
dray
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 03, 2011 12:15 pm

Re: Example for Restful web service authentication

Postby darawalee » Mon Aug 08, 2011 5:36 pm

million thank!!!
darawalee
Freshman
Freshman
 
Posts: 2
Joined: Sat Aug 06, 2011 9:58 pm

Re: Example for Restful web service authentication

Postby James Andrew » Tue Aug 09, 2011 8:48 am

Thanks for posting your code here, Dray. I have also struggled from similar kind of problem.
James Andrew
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Jun 22, 2011 10:17 am

Re: Example for Restful web service authentication

Postby jhogue » Wed Aug 10, 2011 12:01 am

Thanks for the example
jhogue
Freshman
Freshman
 
Posts: 2
Joined: Tue Aug 09, 2011 11:28 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests