Android 中HttpURLConnection與HttpClient使用的簡單實(shí)例
更新時(shí)間:2013年10月15日 16:52:20 作者:
這篇文章介紹了Android 中HttpURLConnection與HttpClient使用的簡單實(shí)例,有需要的朋友可以參考一下
1:HttpHelper.java
復(fù)制代碼 代碼如下:
public class HttpHelper {
//1:標(biāo)準(zhǔn)的Java接口
public static String getStringFromNet1(String param){
String result="";
try{
URL url=new URL(param);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStream is=conn.getInputStream();
byte[]data=new byte[1024];
int len=is.read(data);
result=new String(data,0,len);
is.close();
conn.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
//2:Apache接口
public static String getStringFromNet2(String param){
String result="";
try{
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(param);
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
result=EntityUtils.toString(response.getEntity());
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
您可能感興趣的文章:
- Android HttpURLConnection下載網(wǎng)絡(luò)圖片設(shè)置系統(tǒng)壁紙
- Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法
- Android基于HttpUrlConnection類的文件下載實(shí)例代碼
- Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- Android使用HttpURLConnection實(shí)現(xiàn)網(wǎng)絡(luò)訪問流程
相關(guān)文章
Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼,水波紋效果大致上可以分為兩種,一種是有界的,一種無界,一起跟隨小編過來看看吧2018-05-05Flutter 自定義Drawer 滑出位置的大小實(shí)例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
這篇文章主要為大家詳細(xì)介紹了Android自定義viewgroup,使用adapter適配數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android開發(fā)之Sqliteopenhelper用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之Sqliteopenhelper用法,實(shí)例分析了SQLiteOpenHelper類操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
本文主要介紹Android RecyclerView 數(shù)據(jù)綁定的資料,這里詳細(xì)說明如何實(shí)現(xiàn) Android RecyclerView的數(shù)據(jù)綁定,并附示例代碼,有需要的小伙伴可以參考下2016-09-09Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法
這篇文章主要介紹了Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法,需要的朋友可以參考下2017-11-11Android模擬器對(duì)應(yīng)的電腦快捷鍵說明
Android模擬器對(duì)應(yīng)的電腦快捷鍵說明,需要的朋友可以參考一下2013-06-06