繁体   English   中英

如何在android中调用Restful web服务

[英]How to call Restful web service in android

任何人都可以帮助我如何将身份验证详细信息发布到一个安静的Web服务并从中获取响应。 我必须发布用户名,IsAuthenticated(即真或假),密码。还要解释网址编码方法。 我在下面显示了我的代码。 我是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;
       }
}

我终于得到了答案,并为我工作正常......我已经发布了下面的工作代码。

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

非常感谢您的所有回复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM