繁体   English   中英

HTTP Put请求Android REST API

[英]HTTP Put request Android REST API

我是Android开发的新手,目前的任务是实现一个简单的登录活动,该活动将验证REST后端服务的电子邮件/密码组合。 我配置了布局,并在活动类中配置了EditText电子邮件和密码的值,但是我非常想尝试如何提出HTTP Put请求...非常感谢您的帮助。 我的课和verifyLogin在下面。

public class LoginActivity extends ActionBarActivity {

public void verifyLogin(View view) {

    EditText emailEditText = (EditText)         findViewById(R.id.editText_email_address);
    EditText passwordEditText = (EditText) findViewById(R.id.editText_password);
    String email = emailEditText.getText().toString();
    String password = passwordEditText.getText().toString();

    boolean loginSuccess = false;
    //URL for backend login service
    URL url = new URL("http://ENDPOINT_LOGIN_URL_HERE");
    //put request for this URL
    HttpPut httpPut = new HttpPut(url);
    httpPut.addHeader("application/xml", "Content_type");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    //response from backend
    HttpResponse response = httpClient.execute(httpPut);

}

PS:我已经做了大量的Google搜索,并且观看了视频并浏览了示例代码。 如果没有别的,一个好的起点将是一个巨大的帮助,甚至是一个寻求帮助的好地方。

我正在使用Apache HttpClient for Android。 Android在SDK中具有此库。 有很多关于此的教程。 检查: http : //www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

基本代码是:

  HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("your url"); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("postFiledName1", "value1")); pairs.add(new BasicNameValuePair("postFiledName2", "value2")); post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); // then do something about the response 

暂无
暂无

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

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