HTTP Put 请求 Android REST API
HTTP Put request Android REST API
我是 Android 开发的新手,我当前的任务是实现一个简单的登录 activity 来验证 email/password 与 REST 后端服务的组合。我已经配置了布局,以及 activity class 中的 EditText 电子邮件和密码的值,但我很难弄清楚如何发出 HTTP Put 请求...任何帮助将不胜感激。我的 class 和 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 搜索,观看了视频并查看了示例代码。如果不出意外,一个好的起点将是一个很大的帮助,甚至是寻求帮助的好地方。
我正在为 Android 使用 Apache HttpClient。 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
我是 Android 开发的新手,我当前的任务是实现一个简单的登录 activity 来验证 email/password 与 REST 后端服务的组合。我已经配置了布局,以及 activity class 中的 EditText 电子邮件和密码的值,但我很难弄清楚如何发出 HTTP Put 请求...任何帮助将不胜感激。我的 class 和 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 搜索,观看了视频并查看了示例代码。如果不出意外,一个好的起点将是一个很大的帮助,甚至是寻求帮助的好地方。
我正在为 Android 使用 Apache HttpClient。 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