手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android 中HttpURLConnection与HttpClient使用的简单实例
Android 中HttpURLConnection与HttpClient使用的简单实例
摘要:1:HttpHelper.java复制代码代码如下:publicclassHttpHelper{//1:标准的Java接口publicsta...

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;

}

}

【Android 中HttpURLConnection与HttpClient使用的简单实例】相关文章:

Android CheckBox 的使用案例分析

Android中gravity与layout_gravity的使用区别分析

Android Mms之:PDU的使用详解

android动态壁纸调用的简单实例

Android用户界面开发之:TextView的使用实例

Android中ActionBar以及menu的代码设置样式

Android界面 NotificationManager使用Bitmap做图标

Android Studio使用教程图文详解

Android中BroadcastReceiver(异步接收广播Intent)的使用

Android实现自定义带文字和图片Button的方法

精品推荐
分类导航