How to call Restful web service in android

2020-05-29 10:48发布

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.

public class LoginActivity extends Activity
{
    String Username;
    String Password;
    String IsAuthenticated;
    String answer;

    @Override
     public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
            try {
                POST(Username,Password,IsAuthenticated);
            } catch (Exception e) {
                e.printStackTrace();
            }
      }


     public String POST(String Username, String IsAuthenticated, String Password) {
         String Returned = null;
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost("http://......./Authenticate");

          try {
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
             // Your DATA
             nameValuePairs.add(new BasicNameValuePair("UserName", "Username"));
             nameValuePairs.add(new BasicNameValuePair("IsAuthenticated", "false"));
             nameValuePairs.add(new BasicNameValuePair("Password", "Password"));

             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity resEntity = response.getEntity();
             Returned = EntityUtils.toString(resEntity);

            System.out.println(Returned);
             Toast.makeText(this, Returned, Toast.LENGTH_LONG).show();
          } catch (ClientProtocolException e) {
             Toast.makeText(this, "There was an issue Try again later", Toast.LENGTH_LONG).show();
          } catch (IOException e) {
             Toast.makeText(this, "There was an IO issue Try again later", Toast.LENGTH_LONG).show();
             e.printStackTrace();
          }

          return Returned;
       }
}

1条回答
放我归山
2楼-- · 2020-05-29 11:20

And I got the answer finally and working fine for me... I have posted the working code below.

    public class LoginActivity extends Activity
{
    String Returned;
    @Override
     public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost post = new HttpPost("http://Your url here/");
              StringEntity str = new StringEntity("Your xml code");
              str.setContentType("application/xml; charset=utf-8");
              str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/xml; charset=utf-8"));
              post.setEntity(str);
              HttpResponse response = httpclient.execute(post);
              HttpEntity entity = response.getEntity();
              Returned = EntityUtils.toString(entity);
              Toast.makeText(this, Returned, Toast.LENGTH_LONG).show();
            } catch ( IOException ioe ) {
             ioe.printStackTrace();
            }
          }
}

thanks a lot for all your responses.

查看更多
登录 后发表回答