Created
September 8, 2013 08:07
-
-
Save chuross/6482808 to your computer and use it in GitHub Desktop.
View・通信の処理を簡単実装できるAndroid-Queryの使い方をまとめた ref: http://qiita.com/chuross/items/00da8311f6dac9b7140b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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のレスポンスが文字列で返ってくる | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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のレスポンスが文字列で返ってくる | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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のレスポンスが文字列で返ってくる | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aQuery.id(R.id.activity_main_button).clicked(new View.OnClickListener() { | |
@Override | |
public void onClick() { | |
aQuery.id(R.id.activity_main_text).text("押されたリス"); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hoge.comのfuga.jpgを読み込んできてImageViewにセットする | |
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// image(対象のURL, メモリキャッシュ, ローカルキャッシュ) | |
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg", true, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// image(対象のURL, メモリキャッシュ, ローカルキャッシュ, 読み込む幅, 読み込み失敗時に表示するリソース) | |
aQuery.id(R.id.activity_main_image).image("http://hoge.com/images/huga.jpg", true, true, 200, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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のレスポンスが文字列で返ってくる | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("押されたリス"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.googlecode.android-query</groupId> | |
<artifactId>android-query</artifactId> | |
<version>0.24.3</version> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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