简体   繁体   English

HTTP Put请求Android REST API

[英]HTTP Put request Android REST API

I am fairly new to Android development, and my current task is to implement a simple login activity that verifies an email/password combination with a REST backend service. 我是Android开发的新手,目前的任务是实现一个简单的登录活动,该活动将验证REST后端服务的电子邮件/密码组合。 I have the layout configured, as well as the values for the EditText email and password in my activity class, but I am very stuck trying to figure out how to make the HTTP Put requests... Any help would be greatly appreciated. 我配置了布局,并在活动类中配置了EditText电子邮件和密码的值,但是我非常想尝试如何提出HTTP Put请求...非常感谢您的帮助。 My class and verifyLogin are below. 我的课和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: I have done a great, great deal of Google searches and have watched videos and looked through sample code. PS:我已经做了大量的Google搜索,并且观看了视频并浏览了示例代码。 If nothing else, a good starting point would be a great help or even a good place to look for help. 如果没有别的,一个好的起点将是一个巨大的帮助,甚至是一个寻求帮助的好地方。

I am using Apache HttpClient for Android. 我正在使用Apache HttpClient for Android。 Android has this library inside the SDK. Android在SDK中具有此库。 There are many tutorials talking about this. 有很多关于此的教程。 Check: http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android 检查: http : //www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

The basic codes are: 基本代码是:

  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