Aynchttp Clientを使ってgetとpost要求を実現します.
Aynchttp Client概要:
Aync Http Clientライブラリ Apacheに基づくHttpClientフレームは、非同期のhttp Clientであり、 すべてのhttp要求はサブスレッドにありますが、calbackが実行するスレッドとこのcalbackを作成するスレッドは同じです.
getとpost要求を実現する:
Aync Http Clientライブラリ Apacheに基づくHttpClientフレームは、非同期のhttp Clientであり、 すべてのhttp要求はサブスレッドにありますが、calbackが実行するスレッドとこのcalbackを作成するスレッドは同じです.
getとpost要求を実現する:
public class MainActivity extends Activity {
private EditText usernameET;
private EditText passwordET;
private AsyncHttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameET = (EditText) this.findViewById(R.id.UsernamET);
passwordET = (EditText) this.findViewById(R.id.PaswordET);
client = new AsyncHttpClient();
}
public void get(View v) throws Exception{
String username = usernameET.getText().toString().trim();
String password = passwordET.getText().toString().trim();
String path = "";
String params = "username=" + URLEncoder.encode(username, "UTF-8") + "&password="+ URLEncoder.encode(password, "UTF-8");
client.get(path+"?"+params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
});
}
public void post(View v){
String username = usernameET.getText().toString().trim();
String password = passwordET.getText().toString().trim();
String path = "";
RequestParams params = new RequestParams();
params.put("username", username);
params.put("password", password);
client.post(path, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
});
}
}
AynchttpClientを使用して、ネットワーク画像の読み込みを実現します.public void onClick(View v){
String path = pathText.getText().toString().trim();
client.get(path, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// TODO Auto-generated method stub
Bitmap image = BitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
Image.setImageBitmap(image);
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_SHORT)
.show();
}
});
}
もちろん、これは基本的な使い方です.後にAynchttpClientのアップロードやダウンロードなどの使い方があります.