一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

手機(jī)軟件開發(fā)公司

 jc楓彩 2013-05-03

1.概念      

      HTTP 協(xié)議可能是現(xiàn)在 Internet 上使用得最多、最重要的協(xié)議了,越來越多的 Java 應(yīng)用程序需要直接通過 HTTP 協(xié)議來訪問網(wǎng)絡(luò)資源。在 JDK 的 java.net 包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能:HttpURLConnection。但是對(duì)于大部分應(yīng)用程序來說,JDK 庫(kù)本身提供的功能還不夠豐富和靈活。

      除此之外,在Android中,androidSDK中集成了ApacheHttpClient模塊,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。使用HttpClient可以快速開發(fā)出功能強(qiáng)大的Http程序。

2.區(qū)別

HttpClient是個(gè)很不錯(cuò)的開源框架,封裝了訪問http的請(qǐng)求頭,參數(shù),內(nèi)容體,響應(yīng)等等,

HttpURLConnection是java的標(biāo)準(zhǔn)類,什么都沒封裝,用起來太原始,不方便,比如重訪問的自定義,以及一些高級(jí)功能等。

 URLConnection

    String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do";  
URL url;
HttpURLConnection uRLConnection;
public UrlConnectionToServer(){

}
    //向服務(wù)器發(fā)送get請(qǐng)求
public String doGet(String username,String password){
String getUrl = urlAddress + "?username="+username+"&password="+password;
try {
url = new URL(getUrl);
uRLConnection = (HttpURLConnection)url.openConnection();
InputStream is = uRLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
uRLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
 
    //向服務(wù)器發(fā)送post請(qǐng)求
public String doPost(String username,String password){
try {
url = new URL(urlAddress);
uRLConnection = (HttpURLConnection)url.openConnection();
uRLConnection.setDoInput(true);
uRLConnection.setDoOutput(true);
uRLConnection.setRequestMethod("POST");
uRLConnection.setUseCaches(false);
uRLConnection.setInstanceFollowRedirects(false);
uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uRLConnection.connect();

DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());
String content = "username="+username+"&password="+password;
out.writeBytes(content);
out.flush();
out.close();

InputStream is = uRLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
uRLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

HTTPClient

String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do";  
public HttpClientServer(){

}

public String doGet(String username,String password){
String getUrl = urlAddress + "?username="+username+"&password="+password;
HttpGet httpGet = new HttpGet(getUrl);
HttpParams hp = httpGet.getParams();
hp.getParameter("true");
//hp.
//httpGet.setp
HttpClient hc = new DefaultHttpClient();
try {
HttpResponse ht = hc.execute(httpGet);
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity he = ht.getEntity();
InputStream is = he.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();

//String str = EntityUtils.toString(he);
System.out.println("========="+response);
return response;
}else{
return "error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
}

public String doPost(String username,String password){
//String getUrl = urlAddress + "?username="+username+"&password="+password;
HttpPost httpPost = new HttpPost(urlAddress);
List params = new ArrayList();
NameValuePair pair1 = new BasicNameValuePair("username", username);
NameValuePair pair2 = new BasicNameValuePair("password", password);
params.add(pair1);
params.add(pair2);

HttpEntity he;
try {
he = new UrlEncodedFormEntity(params, "gbk");
httpPost.setEntity(he);

} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

HttpClient hc = new DefaultHttpClient();
try {
HttpResponse ht = hc.execute(httpPost);
//連接成功
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity het = ht.getEntity();
InputStream is = het.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();

//String str = EntityUtils.toString(he);
System.out.println("=========&&"+response);
return response;
}else{
return "error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
}

servlet端json轉(zhuǎn)化: 

        resp.setContentType("text/json");  
resp.setCharacterEncoding("UTF-8");
toDo = new ToDo();
List<UserBean> list = new ArrayList<UserBean>();
list = toDo.queryUsers(mySession);
String body;

//設(shè)定JSON
JSONArray array = new JSONArray();
for(UserBean bean : list)
{
JSONObject obj = new JSONObject();
try
{
obj.put("username", bean.getUserName());
obj.put("password", bean.getPassWord());
}catch(Exception e){}
array.add(obj);
}
pw.write(array.toString());
System.out.println(array.toString());

android端接收:

String urlAddress = "http://192.168.1.102:8080/qualityserver/result.do";  
String body = getContent(urlAddress);
JSONArray array = new JSONArray(body);
for(int i=0;i<array.length();i++)
{
obj = array.getJSONObject(i);
sb.append("用戶名:").append(obj.getString("username")).append("\t");
sb.append("密碼:").append(obj.getString("password")).append("\n");

HashMap<String, Object> map = new HashMap<String, Object>();
try {
userName = obj.getString("username");
passWord = obj.getString("password");
} catch (JSONException e) {
e.printStackTrace();
}
map.put("username", userName);
map.put("password", passWord);
listItem.add(map);

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(sb!=null)
{
showResult.setText("用戶名和密碼信息:");
showResult.setTextSize(20);
} else
extracted();

//設(shè)置adapter
SimpleAdapter simple = new SimpleAdapter(this,listItem,
android.R.layout.simple_list_item_2,
new String[]{"username","password"},
new int[]{android.R.id.text1,android.R.id.text2});
listResult.setAdapter(simple);

listResult.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int positionId = (int) (id+1);
Toast.makeText(MainActivity.this, "ID:"+positionId, Toast.LENGTH_LONG).show();

}
});
}
private void extracted() {
showResult.setText("沒有有效的數(shù)據(jù)!");
}
//和服務(wù)器連接
private String getContent(String url)throws Exception{
StringBuilder sb = new StringBuilder();
HttpClient client =new DefaultHttpClient();
HttpParams httpParams =client.getParams();

HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity =response.getEntity();

if(entity !=null){
BufferedReader reader = new BufferedReader(new InputStreamReader
(entity.getContent(),"UTF-8"),8192);
String line =null;
while ((line= reader.readLine())!=null){
sb.append(line +"\n");
}
reader.close();
}
return sb.toString();
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    男人和女人草逼免费视频| 东京热加勒比一区二区三区| 久久夜色精品国产高清不卡| 丁香七月啪啪激情综合| 香蕉久久夜色精品国产尤物| 91爽人人爽人人插人人爽| 精品国产日韩一区三区| 韩日黄片在线免费观看| 伊人久久青草地综合婷婷| 亚洲国产性感美女视频| 色一情一乱一区二区三区码| 日本高清一区免费不卡| 国产亚洲神马午夜福利| 久久国产精品熟女一区二区三区| 国产丝袜极品黑色高跟鞋| 亚洲国产欧美精品久久| 丁香七月啪啪激情综合| 亚洲妇女黄色三级视频| 日韩色婷婷综合在线观看| 亚洲天堂精品在线视频| 欧美一区二区三区99| 精品人妻一区二区三区免费看 | 色婷婷视频国产一区视频| 国产精品久久男人的天堂| 在线观看视频国产你懂的| 偷拍偷窥女厕一区二区视频| 国产一区二区精品高清免费| 在线一区二区免费的视频| 亚洲香艳网久久五月婷婷| 国产精品久久久久久久久久久痴汉| 丝袜av一区二区三区四区五区| 福利一区二区视频在线| 暴力性生活在线免费视频| 东北女人的逼操的舒服吗| 亚洲成人久久精品国产| 国产真人无遮挡免费视频一区| 亚洲一区二区三区三区| 亚洲乱妇熟女爽的高潮片| 久久精品国产99精品亚洲| 91欧美日韩一区人妻少妇| 国产美女网红精品演绎|