我是Android开发的新手,我目前的任务是实现一个简单的登录活动,通过REST后端服务验证电子邮件/密码组合。我已经配置了布局,以及活动类中EditText电子邮件和密码的值,但是我很难弄清楚如何发出HTTP请求.任何帮助都将不胜感激。我的班和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搜索,看过视频,看过示例代码。如果没有别的,一个好的起点将是一个伟大的帮助,甚至是一个寻求帮助的好地方。
发布于 2015-03-08 05:44:04
我正在使用Apache HttpClient为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
https://stackoverflow.com/questions/28923385
复制相似问题