一种是HttpURLConnection 方法:

public String connect() throws IOException{URL url;url = new URL("http://192.168.1.109/SmartySpace/ZJClub/query.php?format=json");// 每个HttpURLConnection实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络HttpURLConnection conn = (HttpURLConnection) url.openConnection();//设置URL请求的方法 conn.setRequestMethod("GET"); //设置一个指定的超时值(以毫秒为单位),该值将在打开到此 URLConnection 引用的资源的通信链接时使用。conn.setConnectTimeout(5 * 1000); //conn.getInputStream()返回从此打开的连接读取的输入流 InputStream in=conn.getInputStream();System.out.println(in.available());ByteArrayOutputStream bout=new ByteArrayOutputStream();int c;while((c=in.read())!=-1){bout.write(c);}byte b[]=bout.toByteArray();in.close();bout.close();return new String(b);}
另一种是apache的HttpClient包中的方法:

HttpClient client=new DefaultHttpClient();//POST urlString posturl="http://192.168.1.109/SmartySpace/ZJClub/query.php?format=json";//建立HttpPost对象HttpPost httpPost=new HttpPost(posturl);//建立一个NameValuePair数组,用于存储欲传递的参数List<BasicNameValuePair> params=new ArrayList<BasicNameValuePair>();//添加参数params.add(new BasicNameValuePair("user", "1"));//设置编码try {httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}//发送Post,并返回一个HttpResponse对象  HttpResponse response = null;try {response = client.execute(httpPost);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}

更多相关文章

  1. Android通过url获取bitmap图片问题
  2. android通过NFC读取卡号
  3. Android(安卓)CardView+RecyclerView实现卡片列表
  4. android去除标题栏-------全屏运行AND 无标题栏 总结
  5. android:文件下载
  6. Android(安卓)源码解析 - ScrollView
  7. 系统命令行运行 android app程序
  8. Android(安卓): 自己写个HTTP框架
  9. android遇到的BUG

随机推荐

  1. 在Ubuntu Linux 安装Python第三方库:NumP
  2. wifidog 源码初分析(2)
  3. Linux系统下Tar文件安装方法
  4. 在linux下安装并使用websocket
  5. 【linux学习笔记】vim命令小结
  6. [嵌入式Linux驱动]S5PV210的蜂鸣器Linux
  7. [置顶] 每天进步一点点——Linux
  8. c中变参函数的理解和编写(hello world引发
  9. Linux编程之《只运行一个实例》
  10. Linux selinux关闭方法和防火墙关闭方法