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. 类和 Json对象
  2. Android开发学习笔记整理(16)-网络请求
  3. Android(安卓)封装http请求的工具类
  4. android http-post方法简单实现
  5. android 之simpleAdapter详解
  6. Android集成okhttp
  7. Android轻量级JSON操作类
  8. 关于Android/java的复杂对象的深拷贝和浅拷贝
  9. android 利用socket 发送Json数据demo

随机推荐

  1. Netcope公司推出面向100GE网络的网络适配
  2. 基于Xilinx Z-7007实现Z-turn SBC简化版
  3. 阿里深度树匹配召回体系演进
  4. 多媒体内容理解在美图社区的应用实践
  5. 详谈pg逻辑复制系列之逻辑复制的搭建(二)
  6. 机器人的眼睛,需要一个Spartan-6 FPGA
  7. 数据驱动的京东精细化消费者运营平台
  8. Opal Kelly公司推出面向FPGA模块的SYZYGY
  9. 移动商城首页的页眉和页脚的布局和flex项
  10. 第三课 选择器、模块化组件、伪类选择