android - 如何在android中调用Restful web服务

标签 android web-services http rest client-server

任何人都可以帮助我如何将身份验证详细信息发布到 restful web 服务并从中获得响应。 我必须发布用户名、IsAuthenticated(即。true 或 false)、密码。还要解释 url 编码方法。 我在下面显示了我的代码。我是 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();
            }
          }
}

非常感谢您的所有回复。

关于android - 如何在android中调用Restful web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5015204/

相关文章:

performance - Content-Length header 与分块编码

java - Android:可滑动 fragment "onTabSelected"

java - SQLite 在不使用循环的情况下获取结果查询中的单行位置

android - 相对布局错误中的线性布局

web-services - 无法从 Azure 中托管的服务器获得响应

java - 如何在 Struts 2 中调用 Web 服务

java - 请求时出现 SoapUI 错误 - NoClassDefFoundError

http - 为什么 SNDBUF 太低会破坏 HTTP?

HTTP/2 推送 promise 行为

android - 调整布局中所有 ImageView 的大小