十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Java代码
创新互联坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。10余年网站建设经验创新互联是成都老牌网站营销服务商,为您提供网站设计、成都做网站、网站设计、HTML5建站、网站制作、成都品牌网站建设、小程序开发服务,给众多知名企业提供过好品质的建站服务。
/* 注意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);
}
}
你只获取接口的每个 obj_name 和speed 值的话,你完全没必要当json来处里,你可以直接截取字符串就可以了。用substring函数
先用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值了