Skip to content

Instantly share code, notes, and snippets.

@chuross
Created September 8, 2013 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuross/6482808 to your computer and use it in GitHub Desktop.
Save chuross/6482808 to your computer and use it in GitHub Desktop.
View・通信の処理を簡単実装できるAndroid-Queryの使い方をまとめた ref: http://qiita.com/chuross/items/00da8311f6dac9b7140b
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
GsonTransformer t = new GsonTransformer();
aq.transformer(t).ajax("http://hoge.com/api/v1/huga/user/detail.php?id=1", Profile.class, new AjaxCallback<Profile>(){
public void callback(String url, Profile profile, AjaxStatus status) {
Gson gson = new Gson();
showResult("GSON Object:" + gson.toJson(profile), status);
}
});
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "dog");
aQuery.ajax("http://hoge.com/api/v1/huga/animal/add.php", params, String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String result, AjaxStatus status) {
// APIのレスポンスが文字列で返ってくる
}
});
// 15分間キャッシュする
long expire = 15 * 60 * 1000;
aQuery.ajax("http://hoge.com/api/v1/huga/animal/list.php?page=1&limit=20", String.class, expire, new AjaxCallback<String>() {
@Override
public void callback(String url, String result, AjaxStatus status) {
// APIのレスポンスが文字列で返ってくる
}
});
long expire = -1;
aQuery.ajax("http://hoge.com/api/v1/huga/animal/list.php?page=1&limit=20", String.class, expire, new AjaxCallback<String>() {
@Override
public void callback(String url, String result, AjaxStatus status) {
// APIのレスポンスが文字列で返ってくる
}
});
AjaxCallback<JSONObject> callback = new AjaxCallback<JSONObject>();
callback.url("http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0").type(JSONObject.class);
aQuery.sync(callback);
JSONObject result = callback.getResult();
AjaxStatus status = callback.getStatus();
AjaxCallback<String> callback = new AjaxCallback<String>();
callback.url("http://www.google.com").type(String.class);
callback.header("Referer", "http://code.google.com/p/android-query/");
callback.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
aQuery.ajax(callback);
AjaxCallback<JSONObject> callback = new AjaxCallback<JSONObject>();
callback.url("http://www.androidquery.com/p/doNothing").type(JSONObject.class);
callback.cookie("hello", "world").cookie("foo", "bar");
aQuery.ajax(callback);
aQuery.id(R.id.activity_main_button).clicked(new View.OnClickListener() {
@Override
public void onClick() {
aQuery.id(R.id.activity_main_text).text("押されたリス");
}
});
// hoge.comのfuga.jpgを読み込んできてImageViewにセットする
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg");
// image(対象のURL, メモリキャッシュ, ローカルキャッシュ)
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg", true, true);
// image(対象のURL, メモリキャッシュ, ローカルキャッシュ, 読み込む幅, 読み込み失敗時に表示するリソース)
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg", true, true, 200, 0);
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg", true, true, 200, 0, new AbstractBitmapAjaxCallback() {
@Override
public Bitmap transform(String url, byte[] data, AjaxStatus status) {
final File file = status.getFile();
final String path = file != null ? file.getAbsolutePath() : null;
return getResizedImage(path, data, 200, false, 0);
}
});
aQuery.ajax("http://hoge.com/api/v1/huga/animal/list.php?page=1&limit=20", String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String result, AjaxStatus status) {
// APIのレスポンスが文字列で返ってくる
}
});
public class GsonTransformer implements Transformer{
public <T> T transform(String url, Class<T> type, String encoding, byte[] data, AjaxStatus status) {
Gson gson = new Gson();
return gson.fromJson(new String(data), type);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContetnViewでレイアウトをセットしておく
setContentView(R.layout.activity_main);
aQuery = new AQuery(this);
// 色・文字サイズ・テキストの内容をTextViewセットする
aQuery.id(R.id.activity_main_text).textColor(Color.RED).textSize(20).text("リス");
// Buttonが押された時に実行するメソッド名を指定する
aQuery.id(R.id.activity_main_button).clicked(this, "onButtonClicked");
}
public void onButtonClicked() {
//ボタンが押された時にTextViewの内容が変更される
aQuery.id(R.id.activity_main_text).text("押されたリス");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail, container, false);
aQuery = new AQuery(getActivity(), view);
aQuery.id(R.id.fragment_detail_text).text("Fragment内でも使える");
return view;
}
<dependency>
<groupId>com.googlecode.android-query</groupId>
<artifactId>android-query</artifactId>
<version>0.24.3</version>
</dependency>
public class Profile{
public String id;
public String name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment