Java 可以从Android应用程序调用Spring控制器Post方法吗
如何使用httpclient从android应用程序调用SpringPost方法?我在spring控制器中编写了一个post方法(下面是代码示例)。我已在清单xml中授予internet权限,并实现了AsyncTask。但是我从我的Web服务中得到了响应Java 可以从Android应用程序调用Spring控制器Post方法吗,java,android,spring,Java,Android,Spring,如何使用httpclient从android应用程序调用SpringPost方法?我在spring控制器中编写了一个post方法(下面是代码示例)。我已在清单xml中授予internet权限,并实现了AsyncTask。但是我从我的Web服务中得到了响应null // post method to call from android application @RequestMapping(method = RequestMethod.POST) public void login() {
null
// post method to call from android application
@RequestMapping(method = RequestMethod.POST)
public void login() {
log.info("post calling from android");
}
技术上是否可行 我已经解决了这个问题
I have solved the problem
Spring Controller Code:
@RequestMapping(method = RequestMethod.POST)
public void login(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.addHeader("IS_VALID", "yes");
}
**In android Activity class:**
public void login(View view) {
Log.d("","This is login method......");
new ReqService().postData("username", "password");
}
**ReqService Class:**
public class ReqService {
public void postData(String username, String password) {
new RmsLogin().execute("http://10.0.1.112:8080/myapp/idm/android-login", username, password); // url of post method in spring controller
}
private class RmsLogin extends AsyncTask<String, Object, Object> {
private Exception exception;
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
String url = params[0];
String username = params[1];
String password = params[2];
HttpPost httppost = new HttpPost(url);
Log.d("httppost =",httppost.getURI().toString());
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String valid = response.getFirstHeader("IS_VALID").getValue();
Log.d("response :", valid);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Object restult) {
//Log.d("response :", String.valueOf(restult));
}
}
}
弹簧控制器代码:
@RequestMapping(method=RequestMethod.POST)
公共无效登录(HttpServletRequest请求,HttpServletResponse响应){
字符串username=request.getParameter(“用户名”);
字符串密码=request.getParameter(“密码”);
回复:addHeader(“有效”、“是”);
}
**在android活动类中:**
公共无效登录(查看){
Log.d(“,”这是登录方法……);
new ReqService().postData(“用户名”、“密码”);
}
**服务等级:**
公共类服务{
public void postData(字符串用户名、字符串密码){
新建RmsLogin()。执行(“http://10.0.1.112:8080/myapp/idm/android-login“,username,password);//spring控制器中post方法的url
}
私有类RmsLogin扩展异步任务{
私人例外;
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
字符串url=params[0];
字符串username=params[1];
字符串密码=参数[2];
HttpPost HttpPost=新的HttpPost(url);
Log.d(“httppost=,httppost.getURI().toString());
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
String valid=response.getFirstHeader(“IS_valid”).getValue();
Log.d(“响应:”,有效);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
捕获(例外e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(对象result){
//Log.d(“响应:”,String.valueOf(result));
}
}
}
Show使用异步任务。您能看到来自其他客户端的日志消息吗