AsyncHttpClientによるjsonの読み出し操作


package com.example.astest;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MainActivity extends Activity {

	private RequestParams params;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());
		setContentView(R.layout.activity_main);

		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);

		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				requestjsonarray();
			}
		});

		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				requeststring();
			}
		});
	}

	/*
	 *   json  
	 */
	public void requestjson() {
		AsyncHttpClient client = new AsyncHttpClient();
		params = new RequestParams();
		params.put("quest", "login");

		client.post("http://172.198.1.50/remote/test.ashx", params,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(JSONObject response) {
						try {
							Toast.makeText(MainActivity.this,
									response.getString("result"),
									Toast.LENGTH_LONG).show();
						} catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(MainActivity.this, "ddd",
									Toast.LENGTH_LONG).show();
						}
					}
				});

	}

	/*
	 *   json    json     {"result": "yes","people": [{"name":
	 * "ligang"},{"name": "limei"},{"name": "xiaoqiang"}]}
	 *      people,  people        
	 */
	public void requestjsonarray() {
		AsyncHttpClient client = new AsyncHttpClient();
		params = new RequestParams();
		params.put("quest", "fuck");
		client.post("http://172.198.1.50/remote/test.ashx", params,
				new JsonHttpResponseHandler() {
					@Override
					public void onSuccess(JSONObject response) {
						try {
							JSONArray array = response.getJSONArray("people");
							StringBuilder stringBuilder = new StringBuilder();
							for (int i = 0; i < array.length(); i++) {
								stringBuilder.append(array.getJSONObject(i)
										.getString("name"));
							}

							Toast.makeText(MainActivity.this,
									stringBuilder.toString(), Toast.LENGTH_LONG)
									.show();
						} catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(MainActivity.this, "ddd",
									Toast.LENGTH_LONG).show();
						}
					}
				});

	}

	/*
	 *      
	 */
	public void requeststring() {

		AsyncHttpClient client = new AsyncHttpClient();
		params = new RequestParams();
		params.put("quest", "fuck");

		client.post("http://172.198.1.50/remote/test.ashx", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String response) {
						try {
							Toast.makeText(MainActivity.this,
									response.toString(), Toast.LENGTH_LONG)
									.show();
						} catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(MainActivity.this, "ddd",
									Toast.LENGTH_LONG).show();
						}
					}
				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}