重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Java代码
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、坡头网络推广、微信小程序定制开发、坡头网络营销、坡头企业策划、坡头品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供坡头建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
/* 注意get和post请求的发送区别
* 1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中;
2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象;
3、通过HttpResponse接口的getEntity方法返回响应信息。
* */
public class TestHttpActivity extends Activity {
private Button btn_get;
private Button btn_post;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_get=(Button)findViewById(R.id.btn_get);
btn_post=(Button)findViewById(R.id.btn_post);
btn_get.setOnClickListener(listener);
btn_post.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_post:
Log.i("TestHttpActivity", "ok");
DefaultHttpClient client = new DefaultHttpClient();
/**NameValuePair是传送给服务器的请求参数 param.get("name") **/
ListNameValuePair list = new ArrayListNameValuePair();
NameValuePair pair1 = new BasicNameValuePair("name", "name0001");
NameValuePair pair2 = new BasicNameValuePair("age", "age0001");
list.add(pair1);
list.add(pair2);
UrlEncodedFormEntity entity=null;
try {
/**设置编码 **/
entity = new UrlEncodedFormEntity(list,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**新建一个post请求**/
HttpPost post = new HttpPost("");
post.setEntity(entity);
HttpResponse response=null;
String strResult="";
try {
/**客服端向服务器发送请求**/
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**请求发送成功,并得到响应**/
if(response.getStatusLine().getStatusCode()==200){
try {
/**读取服务器返回过来的json字符串数据**/
strResult = EntityUtils.toString(response.getEntity());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
/**把json字符串转换成json对象**/
jsonObject = getJSON(strResult);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String names="";
try {
/**
* jsonObject.getString("code") 取出code
* 比如这里返回的json 字符串为 [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]]
* **/
/**得到data这个key**/
String data=jsonObject.getString("data");
/**把data下的数据转换成json对象**/
JSONObject jDat = new JSONObject(data);
/**判断data对象下的list是否存在**/
if(jDat.get("list")!=null){
/**把list转换成jsonArray对象**/
JSONArray jarr = jDat.getJSONArray("list");
/**循环list对象**/
for (int i = 0; i jarr.length(); i++) {
/** **/
JSONObject jsono = (JSONObject) jarr.get(i);
/**取出list下的name的值 **/
names=names+jsono.getString("name");
}
}
Toast.makeText(TestHttpActivity.this, "code:"+jsonObject.getString("code")+"name:"+names, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else Toast.makeText(TestHttpActivity.this, "POST提交失败", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_get:
DefaultHttpClient client1 = new DefaultHttpClient();
/**NameValuePair是传送给服务器的请求参数 param.get("name") **/
UrlEncodedFormEntity entity1=null;
/**新建一个get请求**/
HttpGet get = new HttpGet("");
HttpResponse response1=null;
String strResult1="";
try {
/**客服端向服务器发送请求**/
response1 = client1.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**请求发送成功,并得到响应**/
if(response1.getStatusLine().getStatusCode()==200){
try {
/**读取服务器返回过来的json字符串数据**/
strResult1 = EntityUtils.toString(response1.getEntity());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject1 = null;
try {
/**把json字符串转换成json对象**/
jsonObject1 = getJSON(strResult1);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String names="";
try {
/**
* jsonObject.getString("code") 取出code
* 比如这里返回的json 字符串为 [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]]
* **/
/**得到data这个key**/
String data=jsonObject1.getString("data");
/**把data下的数据转换成json对象**/
JSONObject jDat1 = new JSONObject(data);
/**判断data对象下的list是否存在**/
if(jDat1.get("list")!=null){
/**把list转换成jsonArray对象**/
JSONArray jarr1 = jDat1.getJSONArray("list");
/**循环list对象**/
for (int i = 0; i jarr1.length(); i++) {
/** **/
JSONObject jsono = (JSONObject) jarr1.get(i);
/**取出list下的name的值 **/
names=names+jsono.getString("name");
}
}
Toast.makeText(TestHttpActivity.this, "get请求: code:"+jsonObject1.getString("code")+"name:"+names, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else Toast.makeText(TestHttpActivity.this, "get提交失败", Toast.LENGTH_SHORT).show();
break;
}
}
};
public JSONObject getJSON(String sb) throws JSONException {
return new JSONObject(sb);
}
}
先用ADO.NET读取数据,然后赋值即可,这个很容易的, 如果你要和某个控件的值做比对,就可以先赋值给控件,然后做比对。。。 例如,你读取数据库中的一条记录,然后绑定到textbox上,和另外一个textbox里面的数字比较大校。
先如下程序集
Imports System.Web.Script.Serialization
Function StringToObject(content As String) As Object
Dim jss As New JavaScriptSerializer()
Return jss.DeserializeObject(content)
End Function
Dim test1 As Dictionary(Of String, Object) = StringToObject("{
" Chr(34) "objects" Chr(34) ": {
" Chr(34) "minecraft/sounds/entity/rabbit/attack2.ogg" Chr(34) ": {
" Chr(34) "hash" Chr(34) ": " Chr(34) "4b90ff3a9b1486642bc0f15da0045d83a91df82e" Chr(34) ",
" Chr(34) "size" Chr(34) ": 7268
},
" Chr(34) "minecraft/sounds/entity/shulker/close5.ogg" Chr(34) ": {
" Chr(34) "hash" Chr(34) ": " Chr(34) "e555fcece121879fe0becbada9d01dea27ab153d" Chr(34) ",
" Chr(34) "size" Chr(34) ": 5376
},
" Chr(34) "minecraft/lang/ga_IE.lang" Chr(34) ": {
" Chr(34) "hash" Chr(34) ": " Chr(34) "a108f19142820b3573a658d40950ba3317b65c15" Chr(34) ",
" Chr(34) "size" Chr(34) ": 142847
},
" Chr(34) "minecraft/sounds/mob/horse/leather.ogg" Chr(34) ": {
" Chr(34) "hash" Chr(34) ": " Chr(34) "20abaa7d3b0baa105bc6023d5308f1e5d76acc41" Chr(34) ",
" Chr(34) "size" Chr(34) ": 11577
}}
}
")
Dim cc As Dictionary(Of String, Object) = DirectCast(test1.Values(0), Dictionary(Of String, Object))
Dim dd = cc.Values(0)("hash")
这样就可以遍历 hash值了
你只获取接口的每个 obj_name 和speed 值的话,你完全没必要当json来处里,你可以直接截取字符串就可以了。用substring函数