Showing posts with label android-query. Show all posts
Showing posts with label android-query. Show all posts

2013-11-05

Android Ajax Authentication using Android-Query and Grails Spring Security Plugin

This post shows code on client end only.

Code in fragment class:
private AQuery aq;
String svalue = null; // keep current session id

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  aq = new AQuery(getActivity(), rootView);

  aq.id(R.id.loginbutton).clicked(this, "onLoginButtonClick");
  aq.id(R.id.logoutbutton).clicked(this, "onLogoutButtonClick");
  aq.id(R.id.dobutton).clicked(this, "onDoButtonClick");
  return rootView;
}


Login:
public void onLoginButtonClick(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/j_spring_security_check";
  Map<string object=""> params = new HashMap<string object="">();
  params.put("j_username", "admin");
  params.put("j_password", "admin");
  params.put("ajax", "true");

  cb = new AjaxCallback<jsonobject>();
  cb.url(url).type(JSONObject.class).params(params).weakHandler(this, "loginCallback");
  cb.header("Content-Type", "application/x-www-form-urlencoded");
  aq.ajax(cb);
}

public void loginCallback(String url, JSONObject json, AjaxStatus status) throws JSONException {
  if(json != null) {
    List<cookie> cookieList = new ArrayList<cookie>();
    cookieList = status.getCookies();
    for (Cookie cookie : cookieList) {
    if(cookie.getName().equals("JSESSIONID")) {
      svalue = cookie.getValue();
    } else {
      svalue = null;
    }
    
    // do something when succeed to log in
  } else {
    // do something when fail to log in
  }
}


Logout:
public void onButton3Click(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/logout/index?ajax=true";
  Map<string object=""> params = new HashMap<string object="">();
  AjaxCallback<string> cb = new AjaxCallback<string>();
  if(svalue != null) cb.cookie("JSESSIONID", svalue);
  cb.url(url).type(JSONObject.class).params(params).weakHandler(this, "logoutCallback");
  aq.ajax(cb);
}

public void logoutCallback(String url, String json, AjaxStatus status) throws JSONException {
  if(json != null){
    // do something when succeed to log out
  }else{
    // do something when fail to log out
  }
}


Do something need to run with or whithout authentication:
public void onDoButtonClick(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/main/doAction?ajax=true";
  AjaxCallback<string> cb = new AjaxCallback<string>();
  cb.url(url).type(JSONObject.class).weakHandler(this, "jsonCallback");
  if(svalue != null) cb.cookie("JSESSIONID", svalue);
  aq.ajax(cb);
}

public void jsonCallback(String url, JSONObject json, AjaxStatus status) throws JSONException {
  if(json != null){
    // do something when succeed to get data from server 
  }else{
    // do something when fail to get data from server
  }
}

2013-11-03

Visit local PC web server from Android application in Genymotion

The URL address used by Android applicaiton in Genymotion should be set to 10.0.3.2, not localhost. For example:

    http://10.0.3.2:8090/GrailsTest1/main/index

2013-10-30

Android程序通过http访问自己PC上的web应用程序,总是返回请求被拒绝

       最近使用Android studio学习Android手机程序开发,并且采用第三方Android-Query库简化界面控件访问和http网络操作。我准备让我的Android程序访问我用Grails编写的很简单的web应用程序,并得到JSON数据。

      访问Grails Web应用的URL为:http://localhost:8090/GrailsTest1/main/index, 在浏览器中访问这个URL可以得到正确的JSON结果, 但我启动Android Genymotion模拟器, 代码中用Android-Query的ajax方法访问始终返回“Network Error”,为更清楚地得到网络错误的具体原因,把AQuery的ajax访问改成Android的HttpClient异步网络调用,得到返回的错误为请求被对方拒绝。

      在网上发帖咨询,有人说可能是CORS的原因,但我发现我的Android代码可以访问ip.jsontest.com并得到正确的JSON结果,如果是CORS的问题,在对客户端程序进行相应CORS的修改前,访问任何web应用都不应该得到结果的,因此排除是CORS问题。

      继续在网上搜索并试验,最终发现了原因:Android模拟器会使用localhost表示自己的地址,而提供另一个特定的IP作为宿主主机地址给Android程序使用,经过测试,这个IP地址是10.0.3.2。我把Android程序中访问web应用的地址改成http://10.0.3.2:8090/GrailsTest1/main/index。奇迹出现了,看到了企盼已久的JSON返回结果。