Thursday, 5 May 2016

Work With AQuery in android


Today I Show You How To Work With AQuery


  • Startup
  • Get Method
  • Post Method
  • Image Loading


  •  Startup :-

compile Gson and Aquery library :-

compile 'com.google.code.gson:gson:2.2.4'

put this file to "libs" folder

https://drive.google.com/open?id=0B1dF4YSBqUooNWZPX0pVXzFtLXc



give 2 permission:-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • Get Method
public void asyncJson()
{
String url = "http://www.your web service url";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {

    @Override
    public void callback(String url, JSONObject json, AjaxStatus status) {
        if(json != null){
Jsonresult jsonResult = new Jsonresult();
JsonElement json2 = new JsonParser()
.parse(json.toString());
Type type = new TypeToken<Jsonresult>() {
}.getType();
jsonResult = new Gson().fromJson(json2,
type);
Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();

        }else{

            //ajax error, show error code
            Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
        }
    }
});
}
  • Post Method
 public void AqueryPost() {
        

        JSONObject obj = new JSONObject();
        try {
            obj.put("UserPassword", "123456");
            obj.put("UserName", "user");
           
            Log.i("json", obj.toString());


        } catch (Exception e) {
         
            return;
        }

        final AQuery aq = new AQuery(signup);
        aq.post("your URL", obj, JSONObject.class, new AjaxCallback<JSONObject>() {

            @Override            public void callback(String url, JSONObject json, AjaxStatus status) {
                ConstantData.progressclose(signup);
                if (json != null) {

                    try {

                        Jsonresult jsonResult = new Jsonresult();
                        JsonElement json2 = new JsonParser()
                                .parse(json.toString());

                        Type type = new TypeToken<Jsonresult>() {
                        }.getType();
                        jsonResult = new Gson().fromJson(json2,
                                type);
//                        if (jsonResult.isFlag()) {

                            if (jsonResult.getMessage().equalsIgnoreCase("success")) {

                        } else
                            Toast.makeText(signup, jsonResult.getMessage(), Toast.LENGTH_LONG).show();


                    } catch (Exception ex) {
                      Toast.makeText(aq.getContext(), "Error :" + json.toString(), Toast.LENGTH_LONG).show();
                    }


                } else {
                    ConstantData.progressclose(signup);
                    //ajax error, show error code  
                  Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
                }
            }
        });

    }




  • Image Loading
public static void SetImage(ImageView imageView) {


    AQuery aq = new AQuery(MainActivity.this);
    AjaxCallback.setNetworkLimit(15);
    BitmapAjaxCallback.setIconCacheLimit(50);
    BitmapAjaxCallback.setCacheLimit(50);
    String urlll = "Your Pic URL";
    aq.id(imageView).image(urlll, false, true, 0,
            R.drawable.background, null, AQuery.FADE_IN);

}








1 comment:

  1. its working perfectly thanx mr wish kalkani for helping me in android

    ReplyDelete