Android 中HttpURLConnection與HttpClient使用的簡單實例
更新時間:2013年10月15日 16:52:20 作者:
這篇文章介紹了Android 中HttpURLConnection與HttpClient使用的簡單實例,有需要的朋友可以參考一下
1:HttpHelper.java
復制代碼 代碼如下:
public class HttpHelper {
//1:標準的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;
}
}
相關文章
Flutter 自定義Drawer 滑出位置的大小實例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
這篇文章主要為大家詳細介紹了Android自定義viewgroup,使用adapter適配數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android開發(fā)之Sqliteopenhelper用法實例分析
這篇文章主要介紹了Android開發(fā)之Sqliteopenhelper用法,實例分析了SQLiteOpenHelper類操作數(shù)據(jù)庫的相關技巧,需要的朋友可以參考下2015-05-05Android RecyclerView 數(shù)據(jù)綁定實例代碼
本文主要介紹Android RecyclerView 數(shù)據(jù)綁定的資料,這里詳細說明如何實現(xiàn) Android RecyclerView的數(shù)據(jù)綁定,并附示例代碼,有需要的小伙伴可以參考下2016-09-09Android實現(xiàn)輸入法彈出時把布局頂上去和登錄按鈕頂上去的解決方法
這篇文章主要介紹了Android實現(xiàn)輸入法彈出時把布局頂上去和登錄按鈕頂上去的解決方法,需要的朋友可以參考下2017-11-11