Java代碼
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、瀍河網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、瀍河網(wǎng)絡(luò)營(yíng)銷、瀍河企業(yè)策劃、瀍河品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供瀍河建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
/* 注意get和post請(qǐng)求的發(fā)送區(qū)別
* 1、創(chuàng)建HttpGet(或HttpPost)對(duì)象,將要請(qǐng)求的URL通過(guò)構(gòu)造方法傳入HttpGet(或HttpPost)對(duì)象中;
2、使用DefaultHttpClient類的execute方法發(fā)送HTTP GET或HTTP POST 請(qǐng)求,并返回HttpResponse對(duì)象;
3、通過(guò)HttpResponse接口的getEntity方法返回響應(yīng)信息。
* */
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是傳送給服務(wù)器的請(qǐng)求參數(shù) 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 {
/**設(shè)置編碼 **/
entity = new UrlEncodedFormEntity(list,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**新建一個(gè)post請(qǐng)求**/
HttpPost post = new HttpPost("");
post.setEntity(entity);
HttpResponse response=null;
String strResult="";
try {
/**客服端向服務(wù)器發(fā)送請(qǐng)求**/
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**請(qǐng)求發(fā)送成功,并得到響應(yīng)**/
if(response.getStatusLine().getStatusCode()==200){
try {
/**讀取服務(wù)器返回過(guò)來(lái)的json字符串?dāng)?shù)據(jù)**/
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字符串轉(zhuǎn)換成json對(duì)象**/
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這個(gè)key**/
String data=jsonObject.getString("data");
/**把data下的數(shù)據(jù)轉(zhuǎn)換成json對(duì)象**/
JSONObject jDat = new JSONObject(data);
/**判斷data對(duì)象下的list是否存在**/
if(jDat.get("list")!=null){
/**把list轉(zhuǎn)換成jsonArray對(duì)象**/
JSONArray jarr = jDat.getJSONArray("list");
/**循環(huán)list對(duì)象**/
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是傳送給服務(wù)器的請(qǐng)求參數(shù) param.get("name") **/
UrlEncodedFormEntity entity1=null;
/**新建一個(gè)get請(qǐng)求**/
HttpGet get = new HttpGet("");
HttpResponse response1=null;
String strResult1="";
try {
/**客服端向服務(wù)器發(fā)送請(qǐng)求**/
response1 = client1.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**請(qǐng)求發(fā)送成功,并得到響應(yīng)**/
if(response1.getStatusLine().getStatusCode()==200){
try {
/**讀取服務(wù)器返回過(guò)來(lái)的json字符串?dāng)?shù)據(jù)**/
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字符串轉(zhuǎn)換成json對(duì)象**/
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這個(gè)key**/
String data=jsonObject1.getString("data");
/**把data下的數(shù)據(jù)轉(zhuǎn)換成json對(duì)象**/
JSONObject jDat1 = new JSONObject(data);
/**判斷data對(duì)象下的list是否存在**/
if(jDat1.get("list")!=null){
/**把list轉(zhuǎn)換成jsonArray對(duì)象**/
JSONArray jarr1 = jDat1.getJSONArray("list");
/**循環(huán)list對(duì)象**/
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請(qǐng)求: 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);
}
}
建立ODBC數(shù)據(jù)源后,可以在Java程序中通過(guò)JDBC-ODBC接口連接數(shù)據(jù)褲:
//SQL Server
DriverManager.getConnection(''jdbc:sqlserver://主機(jī)\\實(shí)例名:端口號(hào);
DatabaseName=數(shù)據(jù)庫(kù)名'',''用戶名'',''密碼'')
看樣子應(yīng)該是設(shè)置和讀取某個(gè)物件的屬性。。而這個(gè)屬性就是sslStatus.Text的值。
在這里Get是將sslStatus.Text這個(gè)結(jié)果讀取出來(lái);而Set(ByVal value As String)是將value這個(gè)參數(shù)值賦值給sslStatus.Text,也即是讓這個(gè)名為sslStatus的文本框顯示value這個(gè)值。