/* Note the difference in sending GET and POST requests * 1. Create an HttpGet (or HttpPost) object, passing the URL to be requested into the HttpGet (or HttpPost) object via its constructor; 2. Use the execute method of the DefaultHttpClient class to send an HTTP GET or HTTP POST request and return an HttpResponse object; 3. Retrieve the response information via the getEntity method of the HttpResponse interface. * */ public class TestHttpActivity extends Activity { private Button btn_get; private Button btn_post; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_get=(Button)findViewById(R.id.btn_get); btn_post=(Button)findViewById(R.id.btn_post); btn_get.setOnClickListener(listener); btn_post.setOnClickListener(listener); } private OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_post: Log.i("TestHttpActivity", "ok"); DefaultHttpClient client = new DefaultHttpClient(); /**NameValuePair is a request parameter sent to the server param.get("name") **/ List<NameValuePair> list = new ArrayList<NameValuePair>(); NameValuePair pair1 = new BasicNameValuePair("name", "name0001"); NameValuePair pair2 = new BasicNameValuePair("age", "age0001"); list.add(pair1); list.add(pair2); UrlEncodedFormEntity entity=null; try { /**Set encoding **/ entity = new UrlEncodedFormEntity(list,"UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } /**Create a new POST request**/ HttpPost post = new HttpPost("http://aaron-pc.wsd.com/Test/testHttp"); post.setEntity(entity); HttpResponse response=null; String strResult=""; try { /**Client sends request to server**/ response = client.execute(post); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /**Request sent successfully, and response received**/ if(response.getStatusLine().getStatusCode()==200){ try { /**Read the JSON string data returned by the server**/ strResult = EntityUtils.toString(response.getEntity()); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject jsonObject = null; try { /**Convert JSON string to JSON object**/ jsonObject = getJSON(strResult); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String names=""; try { /** * jsonObject.getString("code") retrieves the code * For example, the JSON string returned here is [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]] * **/ /**Get the 'data' key**/ String data=jsonObject.getString("data"); /**Convert the data under 'data' to a JSON object**/ JSONObject jDat = new JSONObject(data); /**Check if 'list' exists under the 'data' object**/ if(jDat.get("list")!=null){ /**Convert 'list' to a JSONArray object**/ JSONArray jarr = jDat.getJSONArray("list"); /**Iterate through the 'list' object**/ for (int i = 0; i < jarr.length(); i++) { /** **/ JSONObject jsono = (JSONObject) jarr.get(i); /**Retrieve the 'name' value under 'list' **/ names=names+jsono.getString("name"); } } Toast.makeText(TestHttpActivity.this, "code:"+jsonObject.getString("code")+"name:"+names, Toast.LENGTH_SHORT).show(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else Toast.makeText(TestHttpActivity.this, "POST submission failed", Toast.LENGTH_SHORT).show(); break; case R.id.btn_get: DefaultHttpClient client1 = new DefaultHttpClient(); /**NameValuePair is a request parameter sent to the server param.get("name") **/ UrlEncodedFormEntity entity1=null; /**Create a new GET request**/ HttpGet get = new HttpGet("http://aaron-pc.wsd.com/Test/testHttp"); HttpResponse response1=null; String strResult1=""; try { /**Client sends request to server**/ response1 = client1.execute(get); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /**Request sent successfully, and response received**/ if(response1.getStatusLine().getStatusCode()==200){ try { /**Read the JSON string data returned by the server**/ strResult1 = EntityUtils.toString(response1.getEntity()); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject jsonObject1 = null; try { /**Convert JSON string to JSON object**/ jsonObject1 = getJSON(strResult1); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String names=""; try { /** * jsonObject.getString("code") retrieves the code * For example, the JSON string returned here is [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]] * **/ /**Get the 'data' key**/ String data=jsonObject1.getString("data"); /**Convert the data under 'data' to a JSON object**/ JSONObject jDat1 = new JSONObject(data); /**Check if 'list' exists under the 'data' object**/ if(jDat1.get("list")!=null){ /**Convert 'list' to a JSONArray object**/ JSONArray jarr1 = jDat1.getJSONArray("list"); /**Iterate through the 'list' object**/ for (int i = 0; i < jarr1.length(); i++) { /** **/ JSONObject jsono = (JSONObject) jarr1.get(i); /**Retrieve the 'name' value under 'list' **/ names=names+jsono.getString("name"); } } Toast.makeText(TestHttpActivity.this, "GET request: code:"+jsonObject1.getString("code")+"name:"+names, Toast.LENGTH_SHORT).show(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else Toast.makeText(TestHttpActivity.this, "GET submission failed", Toast.LENGTH_SHORT).show(); break; } } }; public JSONObject getJSON(String sb) throws JSONException { return new JSONObject(sb); } }