如何在android中使用AQuery从服务器端获取数据?

时间:2015-07-27 09:35:36

标签: android

我想从服务器端获取数据,之前我使用Asynctask从服务器端获取数据,但是当我想到了aquery现在我想使用aquery库获取数据。我的服务器页面只返回一个字符串但是通过使用aquery我没有得到响应,它在结果中显示为null。请让我知道有关水上运动的功能,并提供一个链接,以获得更多关于aquery的信息。

这是我用来从服务器获取数据......

aqu=new AQuery(getApplicationContext());
        aqu.ajax("here goes my url", String.class, new AjaxCallback<String>() {

            @Override
            public void callback(String url, String html, AjaxStatus status) {
                  Log.d("here you go",html+"////////////////////"+url); 

            }

    });

在html变量中我得到空值,因为这应该是一个正确的响应,但我无法得到响应。

2 个答案:

答案 0 :(得分:1)

这会对你有所帮助。使用Aquery调用webservice .. http://programmerguru.com/android-tutorial/android-json-parsing-tutorial-using-aquery/

<强>更新 对教程中的函数进行以下更改:

public void updateCitySpinnerCtrl(String country) {
        //JSON URL
        String url = "http://apps.programmerguru.com/json/getcity.php?country="+ country;
        //Make Asynchronous call using AJAX method
        aq.progress(R.id.progressBar1).ajax(url, **String.class,** this,"jsonCallback");
    }

    public void jsonCallback(String url, **String** **res**, AjaxStatus status) {
        //When JSON is not null
        Log.i("json="+res,"response");
        if (json != null) {
            //write you code here
        } 
}

答案 1 :(得分:0)

首先下载aquery jar库并放入android项目的libs文件夹并添加为库并调用以下流程来获取响应。

try {
    AQuery aq = new AQuery(ctx);

    aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
        @Override
        public void callback(String url, JSONObject object, AjaxStatus status) {

            int code = status.getCode();

            try {

                if (code == AjaxStatus.NETWORK_ERROR) {

                    response = null;                            
                    return;

                } else if (code == AjaxStatus.TRANSFORM_ERROR) {

                    response = null;                            
                    return;


                } else if (code == 200) {



                }else{


                    return;

                }

                String result = object.toString();
                Log.e("result:", "" + result);
                if (result.equals("")) {


                } else {


                    try {

                        String sres = new String(result.getBytes(), "UTF-8");
                        response = sres;


                    } catch (UnsupportedEncodingException e) {

                        Log.e("utf8", "conversion", e);


                    } catch (Exception e) {

                        e.printStackTrace();


                    }

                }

            } catch (Exception e) {

                e.printStackTrace();

            }
        }

    });

}catch (Exception e){

    e.printStackTrace();

}