如何调用Android的RESTful Web服务 [英] How to call Restful web service in android

查看:95
本文介绍了如何调用Android的RESTful Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人帮我如何发布身份验证信息到一个RESTful Web服务,并从它那里得到回应。 我要发表用户名,IsAuthenticated(即真或假),Password.Also解释URL编码方法了。 我已经表明我下面的code。我在Android的初学者。

 公共类LoginActivity扩展活动
{
    字符串用户名;
    字符串密码;
    字符串IsAuthenticated;
    字符串的答案;

    @覆盖
     公共无效的onCreate(包savedInstanceState){
           super.onCreate(savedInstanceState);
           的setContentView(R.layout.main);
            尝试 {
                POST(用户名,密码,IsAuthenticated);
            }赶上(例外五){
                e.printStackTrace();
            }
      }


     公共字符串POST(用户名字符串,字符串IsAuthenticated,字符串密码){
         返回的字符串。= NULL;
          HttpClient的HttpClient的=新DefaultHttpClient();
          HttpPost httppost =新HttpPost(HTTP://......./Authenticate);

          尝试 {
             名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
             //你的数据
             nameValuePairs.add(新BasicNameValuePair(用户名,用户名));
             nameValuePairs.add(新BasicNameValuePair(IsAuthenticated,假));
             nameValuePairs.add(新BasicNameValuePair(密码,密码));

             httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中,HTTP.UTF_8));
             HTT presponse响应= httpclient.execute(httppost);
             HttpEntity resEntity = response.getEntity();
             返回= EntityUtils.toString(resEntity);

            的System.out.println(返回);
             Toast.makeText(这一点,回来了,Toast.LENGTH_LONG).show();
          }赶上(ClientProtocolException E){
             Toast.makeText(这一点,有一个问题,请稍后再试,Toast.LENGTH_LONG).show();
          }赶上(IOException异常E){
             Toast.makeText(这一点,有一个IO问题,请稍后再试,Toast.LENGTH_LONG).show();
             e.printStackTrace();
          }

          所得的返还;
       }
}
 

解决方案

和我得到的答案终于和工作正常,我......我已经发布了以下工作code。

 公共类LoginActivity扩展活动
{
    返回的字符串;
    @覆盖
     公共无效的onCreate(包savedInstanceState){
           super.onCreate(savedInstanceState);
           的setContentView(R.layout.main);

          尝试 {
              HttpClient的HttpClient的=新DefaultHttpClient();
              HttpPost后=新HttpPost(HTTP://你这里的网址/);
              StringEntity海峡=新StringEntity(你的XML code);
              str.setContentType(应用程序/ XML;字符集= UTF-8);
              str.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,应用程序/ XML;字符集= UTF-8));
              post.setEntity(STR);
              HTT presponse响应= httpclient.execute(后);
              HttpEntity实体= response.getEntity();
              返回= EntityUtils.toString(实体);
              Toast.makeText(这一点,回来了,Toast.LENGTH_LONG).show();
            }赶上(IOException异常IOE){
             ioe.printStackTrace();
            }
          }
}
 

非常感谢您的所有答复。

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;
       }
}

解决方案

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.

这篇关于如何调用Android的RESTful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆