android发送json并解析返回json
来源:互联网 发布:数据记录表 编辑:程序博客网 时间:2024/05/01 03:52
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) {
// 异常处理代码
}
}
});
}
}
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) {
// 异常处理代码
}
}
});
}
}
- android发送json并解析返回json
- android发送json并解析返回json
- 返回并解析json
- Android发送http请求并解析json
- Android使用HttpURLConnection请求网络返回JSON数据并解析
- Android 接口返回Json解析
- http请求返回并解析json数据
- http请求返回并解析json数据
- AJAX向服务器发送请求并返回json数据
- 【json】android json解析
- Android解析服务器返回的Json数据
- Python发送http请求解析返回json小demo
- client提交json(nodejs)和server解析json并返回json数据(express)
- android访问服务器并解析返回的XML和JSON数据
- android 客户端访问自己建立的服务器并返回JSON数据进行解析学习
- 初探Android中的请求服务器并解析返回的json数据
- HttpClient 发送Json请求,结果返回Json
- httpclient发送Json请求,结果返回Json.
- C++错误:重定义 不同的存储类 二次定义的解决方法
- ubuntu 包管理之“:E: 读错误 - read (5: 输入/输出错误) E: 无法解析或打开软件包的列表或是状态文件。”的解决
- 64位服务器IIS程序发布
- Android 权限大全
- 开源社区的管理模式
- android发送json并解析返回json
- Java int 与 string的转换
- love will keep us alive & yellow
- C# winfrom获取外网IP地址和物理地址
- php实现大整数加法
- LUCENE创建索引【鸡蛋】
- cocos2d开发工具大全
- loadrunner9.5x破解完全教程
- JAVA组合模式应用-树形结构