2014-12-03 80 views
0

如何使用httpclient从android应用程序调用spring post方法?我在我的弹簧控制器(代码示例波纹管)中写了一个post方法。我已经在manifest xml中提供了Internet权限并实现了AsyncTask。但我从我的webservice获得响应null是否有可能从Android应用程序调用Spring Controller Post方法

// post method to call from android application 
@RequestMapping(method = RequestMethod.POST) 
public void login() { 
    log.info("post calling from android"); 
} 

技术上可行吗?

+0

显示使用您的AsyncTask。你能看到来自其他客户端的日志消息吗? – 2014-12-03 11:28:54

回答

1
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)); 
     } 

    } 

} 
相关问题