1 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.node07">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TEST">
<activity android:name=".PipeActivity"></activity>
<activity android:name=".PipeCountActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.通过netUtil调用doget或dopost
String (String city) { String weatherUrl = + + + + + + + + cityLog.(+ weatherUrl)String weatherResult = (weatherUrl)(weatherResult)}
com.example.node07.utilandroid.util.Logjava.io.BufferedReaderjava.io.IOExceptionjava.io.InputStreamjava.io.InputStreamReaderjava.io.OutputStreamjava.net.HttpURLConnectionjava.net.URLjava.nio.charset.StandardCharsetsjava.util.HashMapjava.util.Mapjava.util.SetNetUtil { String = String = String = String (String url) { HttpURLConnection urlConnection = BufferedReader reader = String bookJSONString = { URL requestURL = URL(url)urlConnection = (HttpURLConnection) requestURL.openConnection()urlConnection.setRequestMethod()urlConnection.setConnectTimeout()urlConnection.setRequestProperty()urlConnection.connect()InputStream inputStream = urlConnection.getInputStream()reader = BufferedReader(InputStreamReader(inputStreamStandardCharsets.))StringBuilder builder = StringBuilder()String line((line = reader.readLine()) != ) { builder.append(line)} (builder.length() == ) { } bookJSONString = builder.toString()} (Exception e) { e.printStackTrace()} { (urlConnection != ) { urlConnection.disconnect()} (reader != ) { { reader.close()} (IOException e) { e.printStackTrace()} } } bookJSONString} (String urlStr) { HttpURLConnection urlConnection = OutputStream outputStream = result = { URL url = URL(urlStr)urlConnection = (HttpURLConnection) url.openConnection()Map<StringString> paramMap = HashMap<>()paramMap.put()paramMap.put()String paramData = (paramMap)urlConnection.setRequestMethod()urlConnection.setConnectTimeout(* )urlConnection.setRequestProperty(String.(paramData.length()))urlConnection.setDoOutput()outputStream = urlConnection.getOutputStream()outputStream.write(paramData.getBytes())code = urlConnection.getResponseCode()(code == ) { result = } } (Exception e) { e.printStackTrace()}{ (urlConnection != ) { urlConnection.disconnect()} (outputStream != ) { { outputStream.close()} (IOException e) { e.printStackTrace()} } } result} String (Map<StringString> paramMap) { StringBuilder sb = StringBuilder()Set<Map.Entry<StringString>> entries = paramMap.entrySet()(Map.Entry<StringString> entry : entries) { sb.append(entry.getKey()) .append() .append(entry.getValue()) .append()} sb.deleteCharAt(sb.length() - )sb.toString()} String (String city) { String weatherUrl = + + + + + + + + cityLog.(+ weatherUrl)String weatherResult = (weatherUrl)(weatherResult)} String (String unicodeStr) { (unicodeStr == ) { } StringBuffer retBuf = StringBuffer()maxLoop = unicodeStr.length()(i = i < maxLoopi++) { (unicodeStr.charAt(i) == ) { ((i < maxLoop - ) && ((unicodeStr.charAt(i + ) == ) || (unicodeStr.charAt(i + ) == ))) { retBuf.append(() Integer.(unicodeStr.substring(i + i + )))i += } (NumberFormatException localNumberFormatException) { retBuf.append(unicodeStr.charAt(i))} retBuf.append(unicodeStr.charAt(i))} { retBuf.append(unicodeStr.charAt(i))} } retBuf.toString()} }