• 网络编程(一)


    get 方法实例:
        String url="http://202.194.14.215/iactivity/mobile/msg.php?act=1";
    			String tixingfanhuizhi=null;
    			
    			DefaultHttpClient client=new DefaultHttpClient();//新建client
    			HttpGet get=new HttpGet(url); //新建httpget
    			
    			HttpResponse response=null;
    			
    			get.setHeader("Cookie",getCookie());//setcookie,有的时候可能用不到
    			response=client.execute(get);
    			
    			tixingfanhuizhi = EntityUtils.toString(response.getEntity(), "UTF-8");
    			Document doc=Jsoup.parse(tixingfanhuizhi);//将string返回值生成document文件,然后就可以用Jsoup解析Html文件了
    			Elements pinglun=doc.select("div");
    			int a=pinglun.size();
    			
    
    post方法实例
    DefaultHttpClient httpclient = new DefaultHttpClient();//创建httpclient List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("user", username)); params.add(new BasicNameValuePair("key", key)); UrlEncodedFormEntity entity = null; try { entity = new UrlEncodedFormEntity(params, "UTF-8"); HttpPost httppost = new HttpPost(url); httppost.setEntity(entity); //用post方法,里面放置参数 try { response = httpclient.execute(httppost);//获得httpresponse Header header = response.getFirstHeader("Set-Cookie"); if (header != null) { httpcookie = header.getValue(); } try { fanhuishuzhi = EntityUtils.toString(response.getEntity(), HTTP.UTF_8); //获得其中的内容 code = Integer.parseInt(fanhuishuzhi);//转换为数据值 } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getApplicationContext(),getResources() .getString(R.string.login_no), Toast.LENGTH_SHORT).show(); dialog.dismiss(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getApplicationContext(),getResources() .getString(R.string.login_no), Toast.LENGTH_SHORT).show(); dialog.dismiss(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getApplicationContext(),getResources() .getString(R.string.login_no), Toast.LENGTH_SHORT).show(); dialog.dismiss(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
  • 相关阅读:
    C# 直接选择排序(史上最清晰,最通俗)
    Hadoop单节点伪分布式环境部署
    Hive安装和部署andMySQL安装和部署
    Kafka集群部署
    HA Hadoop完全分布式环境部署
    HBase集群部署
    Flume整合Kafka
    Hadoop完全分布式环境部署
    JavaScript正则表达式语法
    什么无线路由器性价高?买什么路由器好?
  • 原文地址:https://www.cnblogs.com/lingxianxia/p/4321053.html
Copyright © 2020-2023  润新知