android json和httpget的使用

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   使用   java   sp   

public class JsonActivity extends Activity {     /** Called when the activity is first created. */     private TextView tvJson;     private Button btnJson;     private Button btnJsonMulti;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         tvJson = (TextView) this.findViewById(R.id.tvJson);         btnJson = (Button) this.findViewById(R.id.btnJson);         btnJsonMulti = (Button) this.findViewById(R.id.btnJsonMulti);         btnJson.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // url                 // String strUrl = "http://10.158.166.110:8080/AndroidServer/JsonServlet";                 String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGER);                 //獲得返回的Json字串                 String strResult = connServerForResult(strUrl);                 //解析Json字串                 parseJson(strResult);             }         });         btnJsonMulti.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGERS);                 String strResult = connServerForResult(strUrl);                 //獲得多個Singer                 parseJsonMulti(strResult);             }         });     }     private String connServerForResult(String strUrl) {         // HttpGet對象         HttpGet httpRequest = new HttpGet(strUrl);         String strResult = "";         try {             // HttpClient對象             HttpClient httpClient = new DefaultHttpClient();             // 獲得HttpResponse對象             HttpResponse httpResponse = httpClient.execute(httpRequest);             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                 // 取得返回的資料                 strResult = EntityUtils.toString(httpResponse.getEntity());             }         } catch (ClientProtocolException e) {             tvJson.setText("protocol error");             e.printStackTrace();         } catch (IOException e) {             tvJson.setText("IO error");             e.printStackTrace();         }         return strResult;     }     // 普通Json資料解析     private void parseJson(String strResult) {         try {             JSONObject jsonObj = new JSONObject(strResult).getJSONObject("singer");             int id = jsonObj.getInt("id");             String name = jsonObj.getString("name");             String gender = jsonObj.getString("gender");             tvJson.setText("ID號"+id + ", 姓名:" + name + ",性別:" + gender);         } catch (JSONException e) {             System.out.println("Json parse error");             e.printStackTrace();         }     }     //解析多個資料的Json    private void parseJsonMulti(String strResult) {         try {             JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("singers");             String s = "";             for(int i = 0; i < jsonObjs.length() ; i++){                 JSONObject jsonObj = ((JSONObject)jsonObjs.opt(i))                 .getJSONObject("singer");                 int id = jsonObj.getInt("id");                 String name = jsonObj.getString("name");                 String gender = jsonObj.getString("gender");                 s +=  "ID號"+id + ", 姓名:" + name + ",性別:" + gender+ "\n" ;             }             tvJson.setText(s);         } catch (JSONException e) {             System.out.println("Jsons parse error !");             e.printStackTrace();         }     } } 

  

android json和httpget的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.