How to call Restful web service in android

后端 未结 1 1684
慢半拍i
慢半拍i 2021-02-06 19:45

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.Als

相关标签:
1条回答
  • 2021-02-06 20:19

    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.

    0 讨论(0)
提交回复
热议问题