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 = "http://10.10.10.10:61002/userMessage/cJobConsultationUnread.json?data=688656&client_id=20012&view_id=268800";
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(httpUrl);
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
strResult = EntityUtils.toString(httpResponse
.getEntity());
tv.setText(strResult);
} else {
tv.setText("请求错误!");
}


} 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) {
// 异常处理代码
}


}


});


}
}

更多相关文章

  1. Android中base64加密后的字符串带有“\n”导致出错的问题解决
  2. %1$s %1$d Android string (java & Android 格式化字符串)
  3. android 使用Intent传递对象 Serializable 或者 Parcelabel 《第
  4. Android中字符串的拆分-split
  5. iphone/android比较学习之──图片、文件、字符串
  6. Android -- SharedPreferences保存基本数据、序列化对象、List数
  7. Android webview与js交换JSON对象数据示例
  8. Android中Intent传递对象的两种方法(Serializable,Parcelable)

随机推荐

  1. android文件系统制作教程
  2. android中menu的使用
  3. Android正确关闭线程
  4. Android(安卓)GPS架构分析
  5. [Xamarin.Android] ActionBar Tips
  6. ubuntu 下 配置 Android(安卓)NDK开发环
  7. 修改EditText的光标颜色
  8. Android(安卓)7.0后下载后自动打开安装包
  9. Android(安卓)Animation开机动画的优化
  10. android handler.removeCallbacksAndMess