码迷,mamicode.com
首页 > 编程语言 > 详细

Java网络编程知识点(1)

时间:2014-06-03 04:49:50      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:java   网络   url   

如何将一个String对象转换成InputStream对象?

ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());

如何替换掉网络URL里面的参数值?

String baseURL = "http://sixweather.3gpk.net/SixWeather.aspx?city=%s";
String cityName = "武汉";
		
String resultURL = String.format(baseURL, cityName);


如何在Java中访问网络地址,获取返回结果?

// **************访问网络,获取天气预报数据,将结果数据存储到result中*******************
				//初始化httpGet,将参数url传给它
				HttpGet httpGet = new HttpGet(url);

				String result = "";
				
				//初始化httpClient
				HttpClient httpClient = new DefaultHttpClient();
				
				//调用httpClient的execute方法,返回一个HttpResponse的实例
				HttpResponse httpResponse = httpClient.execute(httpGet);

				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					//将httpResponse中的天气数据转换成字符串形式
					result = EntityUtils.toString(httpResponse.getEntity());
				}

// ****************************************************************************************

如何在Java中对字符串String进行编码、解码操作?

		String cityName = "武汉";
		
		try {
			//对cityName进行utf-8编码操作
			cityName = URLEncoder.encode(cityName, "utf-8");
			
			//对cityName进行utf-8解码操作
			cityName = URLDecoder.decode(cityName, "utf-8");
			
		} catch (Exception e) {
			e.printStackTrace();
		}


Java网络编程知识点(1),布布扣,bubuko.com

Java网络编程知识点(1)

标签:java   网络   url   

原文地址:http://blog.csdn.net/bear_huangzhen/article/details/25588839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!