android發送json并解析返回json

openkk 12年前發布 | 43K 次閱讀 Android Android開發 移動開發

package com.http.test;

import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; //import android.widget.EditText; import android.widget.TextView;

public class Http_testActivity extends Activity { /* Called when the activity is first created. / @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

final TextView tv = (TextView) findViewById(R.id.result); //final EditText ed = (EditText) findViewById(R.id.sendurl); Button bt = (Button) findViewById(R.id.send);

bt.setOnClickListener(new OnClickListener() {// 創建第一個單擊事件

public void onClick(View v) {

String strResult = null;

try { String httpUrl = "

} catch (Exception e) {

}

//返回的json串strResult={"status":0,"message":"OK","data":15} try {

JSONTokener jsonParser = new JSONTokener(strResult);
JSONObject js = (JSONObject) jsonParser.nextValue();
// 接下來的就是JSON對象的操作了
System.out.println("status的值是:"+js.getString("status"));
System.out.println("message的值是:"+js.getString("message"));
System.out.println("data的值是:"+js.getInt("data"));

} catch (JSONException ex) {
// 異常處理代碼
}

}

});

} } </pre>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!