Java Network programming knowledge point (1)

Source: Internet
Author: User

How to convert a String object to an InputStream object?

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

How do I replace the parameter values in the network URL?

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


In Java, how does one access the network address and obtain the returned results?

// *************** Access the network to obtain weather forecast data, store result data to result ********************* // initialize httpGet, pass the url parameter to HttpGet httpGet = new HttpGet (url); String result = ""; // initialize httpClientHttpClient httpClient = new DefaultHttpClient (); // call the execute method of httpClient, returns the HttpResponse instance HttpResponse httpResponse = httpClient.exe cute (httpGet); if (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// convert the weather data in httpResponse to the string format result = EntityUtils. toString (httpResponse. getEntity ());} //************************************** **************************************** **********

How to encode and decode String in Java?

String cityName = "Wuhan"; try {// perform UTF-8 encoding for cityName = URLEncoder. encode (cityName, "UTF-8"); // perform UTF-8 Decoding for cityName. cityName = URLDecoder. decode (cityName, "UTF-8");} catch (Exception e) {e. printStackTrace ();}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.