Java网络编程知识点(1)

怎样将一个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();
		}


转载于:https://www.cnblogs.com/lxjshuju/p/6887948.html

你可能感兴趣的:(Java网络编程知识点(1))