{"id":925,"date":"2015-08-05T19:32:03","date_gmt":"2015-08-05T11:32:03","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=925"},"modified":"2016-09-02T15:07:32","modified_gmt":"2016-09-02T07:07:32","slug":"http%e7%bd%91%e7%bb%9c%e8%bf%9e%e6%8e%a5%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=925","title":{"rendered":"Http\u7f51\u7edc\u8fde\u63a5\u5904\u7406"},"content":{"rendered":"<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u5b66\u4e60Android\u7a0b\u5e8f\u5982\u4f55\u8fdb\u884c\u7f51\u7edc\u8fde\u63a5\uff0c\u83b7\u53d6\u7f51\u7edc\u8fd4\u56de\u6570\u636e\uff0c\u5982\u679c\u5bf9HTTP\u7f51\u7edc\u534f\u8bae\u8fd8\u4e0d\u4e86\u89e3\u7684\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/www.360doc.com\/content\/10\/0930\/17\/3668821_57590979.shtml\">\u6df1\u5165\u7406\u89e3HTTP\u534f\u8bae<\/a>\uff0c\u5728\u8fdb\u884c\u7f51\u7edc\u8fde\u63a5\u64cd\u4f5c\u524d\u9700\u8981\u5148\u5728\u7a0b\u5e8f\u7684manifest\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e0b\u9762\u7684permissions:<\/p>\n<p><!--more--><\/p>\n<p>[java]<br \/>\n&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt;<br \/>\n&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; \/&gt;<br \/>\n[\/java]<\/p>\n<p>\u540c\u65f6\u8981\u68c0\u67e5\u5f53\u524d\u7f51\u7edc\u73af\u5883\u662f\u5426\u53ef\u7528<\/p>\n<p>[java]<br \/>\nConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);<br \/>\nNetworkInfo networkInfo = connMgr.getActiveNetworkInfo();<br \/>\nif (isGpsConnection() || isWiFiConnection()) {<br \/>\n    \/\/\u7f51\u7edc\u8fde\u63a5\u53ef\u7528<br \/>\n} else {<br \/>\n    \/\/\u7f51\u7edc\u8fde\u63a5\u4e0d\u53ef\u7528!<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>[java] \/**<br \/>\n     * \u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u662f\u5426\u53ef\u7528<br \/>\n     *\/<br \/>\n    public boolean isGpsConnection(){<br \/>\n        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);<br \/>\n        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();<br \/>\n        if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {<br \/>\n            \/\/\u7f51\u7edc\u8fde\u63a5\u53ef\u7528<br \/>\n            return true;<br \/>\n        } else {<br \/>\n            \/\/\u7f51\u7edc\u8fde\u63a5\u4e0d\u53ef\u7528!<br \/>\n            return false;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public boolean isWiFiConnection(Context inContext) {<br \/>\n        Context context = inContext.getApplicationContext();<br \/>\n        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);<\/p>\n<p>        if (connectivity != null) {<br \/>\n            NetworkInfo[] info = connectivity.getAllNetworkInfo();<br \/>\n            if (info != null) {<br \/>\n                for (int i = 0; i &lt; info.length; i++) {<br \/>\n                    if (info[i].getTypeName().equals(&quot;WIFI&quot;) &amp;&amp; info[i].isConnected()) {<br \/>\n                        return true;<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return false;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>\u7f51\u7edc\u64cd\u4f5c\u4f1a\u9047\u5230\u4e0d\u53ef\u9884\u671f\u7684\u5ef6\u8fdf\u3002\u663e\u7136\u4e3a\u4e86\u907f\u514d\u4e00\u4e2a\u4e0d\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u603b\u662f\u5728UI Thread\u4e4b\u5916\u53bb\u6267\u884c\u7f51\u7edc\u64cd\u4f5c\u3002AsyncTask \u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898<br \/>\ndoInBackground()) \u6267\u884c downloadUrl()\u65b9\u6cd5\u3002Web URL\u4f5c\u4e3a\u53c2\u6570\uff0c\u65b9\u6cd5downloadUrl() \u83b7\u53d6\u5e76\u5904\u7406\u7f51\u9875\u8fd4\u56de\u7684\u6570\u636e\uff0c\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f20\u9012\u7ed3\u679c\u5230onPostExecute()\u3002\u53c2\u6570\u7c7b\u578b\u4e3aString.<br \/>\nonPostExecute()) \u83b7\u53d6\u5230\u8fd4\u56de\u6570\u636e\u5e76\u663e\u793a\u5230UI\u4e0a\u3002<\/p>\n<p>[java]private class DownloadWebpageText extends AsyncTask&lt;String, Void, String&gt; {<br \/>\n\t\t@Override<br \/>\n\t\tprotected String doInBackground(String&#8230; urls) {<\/p>\n<p>\t\t\t\/\/ \u53c2\u6570\u4eceexecute()\u65b9\u6cd5\u4f20\u5165, params[0]\u8868\u793aurl.<br \/>\n\t\t\t\/\/\u83b7\u53d6url\u8d44\u6e90\u6570\u636e\u5e76\u8fd4\u56de<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ onPostExecute\u65b9\u6cd5\u4e2d\u5904\u7406AsyncTask\u8fd4\u56de\u7684\u7ed3\u679c<br \/>\n\t\t@Override<br \/>\n\t\tprotected void onPostExecute(String result) {<\/p>\n<p>\t\t}<\/p>\n<p>\t}<br \/>\n[\/java]<\/p>\n<p>\u6211\u4eec\u6765\u770b\u4e0b\u5b8c\u6574\u7684\u5b9e\u4f8b\u4ee3\u7801<br \/>\n\u5e03\u5c40\u6587\u4ef6activity_httpconnection_layout.xml<\/p>\n<p>[java]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:layout_width=&quot;match_parent&quot;<br \/>\n    android:layout_height=&quot;match_parent&quot;<br \/>\n    android:orientation=&quot;vertical&quot; &gt;<\/p>\n<p>    &lt;EditText<br \/>\n        android:id=&quot;@+id\/http_url_edit&quot;<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:hint=&quot;\u8bf7\u8f93\u5165URL\u5730\u5740&quot; \/&gt;<\/p>\n<p>    &lt;TextView<br \/>\n        android:id=&quot;@+id\/http_content_txt&quot;<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot; \/&gt;<\/p>\n<p>    &lt;Button android:id=&quot;@+id\/http_get_content_btn&quot;<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:onClick=&quot;onConnectionClickHandler&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot; \/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;[\/java]<\/p>\n<p>\u539f\u4ee3\u7801 HttpExampleActivity.java<\/p>\n<p>[java]<br \/>\npublic class HttpExampleActivity extends Activity {<br \/>\n\tprivate static final String DEBUG_TAG = &quot;HttpExample&quot;;<br \/>\n\tprivate EditText mUrlText;<br \/>\n\tprivate TextView mShowMessageTxt;<\/p>\n<p>\t@Override<br \/>\n\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\tsetContentView(R.layout.activity_httpconnection_layout);<br \/>\n\t\tmUrlText = (EditText) findViewById(R.id.http_url_edit);<br \/>\n\t\tmShowMessageTxt = (TextView) findViewById(R.id.http_content_txt);<br \/>\n\t}<\/p>\n<p>\t\/**<br \/>\n\t * \u7528\u6237\u70b9\u51fb\u6b64\u6309\u94ae\u65f6\u6267\u884c\u5f02\u6b65\u4efb\u52a1\uff0c\u5728\u6267\u884c\u524d\u9700\u68c0\u67e5\u7f51\u7edc\u662f\u5426\u53ef\u7528.<br \/>\n\t *<br \/>\n\t *\/<br \/>\n\tpublic void onConnectionClickHandler(View view) {<br \/>\n\t\tString stringUrl = mUrlText.getText().toString();\/\/ \u83b7\u53d6\u8bf7\u6c42\u7684URL\u4eceEditText\u63a7\u4ef6\u4e2d.<br \/>\n\t\tConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);<br \/>\n\t\tNetworkInfo networkInfo = connMgr.getActiveNetworkInfo();<br \/>\n\t\tif (networkInfo != null &amp;&amp; networkInfo.isConnected()) {<br \/>\n\t\t\tnew DownloadWebpageText().execute(stringUrl);<br \/>\n\t\t} else {<br \/>\n\t\t\tmShowMessageTxt.setText(&quot;\u7f51\u7edc\u8fde\u63a5\u4e0d\u53ef\u7528!&quot;);<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tprivate class DownloadWebpageText extends AsyncTask&lt;String, Void, String&gt; {<br \/>\n\t\t@Override<br \/>\n\t\tprotected String doInBackground(String&#8230; urls) {<\/p>\n<p>\t\t\t\/\/ \u53c2\u6570\u4eceexecute()\u65b9\u6cd5\u4f20\u5165, params[0]\u8868\u793aurl.<br \/>\n\t\t\ttry {<br \/>\n\t\t\t\treturn downloadUrl(urls[0]);<br \/>\n\t\t\t} catch (IOException e) {<br \/>\n\t\t\t\treturn &quot;Unable to retrieve web page. URL may be invalid.&quot;;<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/ onPostExecute\u65b9\u6cd5\u4e2d\u5904\u7406AsyncTask\u8fd4\u56de\u7684\u7ed3\u679c<br \/>\n\t\t@Override<br \/>\n\t\tprotected void onPostExecute(String result) {<br \/>\n\t\t\tmShowMessageTxt.setText(result);<br \/>\n\t\t}<\/p>\n<p>\t}<\/p>\n<p>\t\/**<br \/>\n\t * \u6839\u636e\u7ed9\u5b9a\u7684url\u5730\u5740\uff0c\u5efa\u7acb\u4e00\u4e2aHttpUrlConnection\u8fde\u63a5\u5b83\u5c06\u4ee5\u6d41\u7684\u5f62\u5f0f\u8fd4\u56de\u9875\u9762\u5185\u5bb9\uff0c\u5c06\u6d41\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u8fd4\u56de<br \/>\n\t * @param myurl<br \/>\n\t * @return<br \/>\n\t * @throws IOException<br \/>\n\t *\/<br \/>\n\tprivate String downloadUrl(String myurl) throws IOException {<br \/>\n\t\tInputStream is = null;<\/p>\n<p>\t\ttry {<br \/>\n\t\t\tURL url = new URL(myurl);<br \/>\n\t\t\tHttpURLConnection conn = (HttpURLConnection) url.openConnection();<br \/>\n\t\t\tconn.setReadTimeout(10000);  \/*\u8bfb\u6570\u636e\u8d85\u65f6*\/<br \/>\n\t\t\tconn.setConnectTimeout(15000);\/*\u8fde\u63a5\u8d85\u65f6*\/<br \/>\n\t\t\tconn.setRequestMethod(&quot;GET&quot;); \/*\u8bf7\u6c42\u65b9\u5f0f*\/<br \/>\n\t\t\tconn.setDoInput(true);\/* \u8bbe\u7f6e\u5141\u8bb8\u8f93\u5165\u6d41*\/<\/p>\n<p>\t\t\tconn.connect();<br \/>\n\t\t\tint response = conn.getResponseCode();<br \/>\n\t\t\tLog.d(DEBUG_TAG, &quot;The response is: &quot; + response);<br \/>\n\t\t\tis = conn.getInputStream();<\/p>\n<p>\t\t\tString contentAsString = readIt(is);<br \/>\n\t\t\treturn contentAsString;<\/p>\n<p>\t\t\t\/\/\u5173\u95edInputStream<br \/>\n\t\t} finally {<br \/>\n\t\t\tif (is != null) {<br \/>\n\t\t\t\tis.close();<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\t\/**<br \/>\n\t * \u5c06InputStream\u8f6c\u6362\u6210String\u8fd4\u56de<br \/>\n\t * @param stream<br \/>\n\t * @return<br \/>\n\t * @throws IOException<br \/>\n\t * @throws UnsupportedEncodingException<br \/>\n\t *\/<br \/>\n\tpublic String readIt(InputStream stream) throws IOException,UnsupportedEncodingException {<br \/>\n\t\tReader reader = new InputStreamReader(stream, &quot;UTF-8&quot;);<br \/>\n\t\t\/\/ \u521b\u5efa\u5305\u88c5\u6d41<br \/>\n\t\tBufferedReader br = new BufferedReader(reader);<br \/>\n\t\t\/\/ \u5b9a\u4e49String\u7c7b\u578b\u7528\u4e8e\u50a8\u5b58\u5355\u884c\u6570\u636e<br \/>\n\t\tString line = null;<br \/>\n\t\t\/\/ \u521b\u5efaStringBuffer\u5bf9\u8c61\u7528\u4e8e\u5b58\u50a8\u6240\u6709\u6570\u636e<br \/>\n\t\tStringBuffer sb = new StringBuffer();<br \/>\n\t\twhile ((line = br.readLine()) != null) {<br \/>\n\t\t\tsb.append(line);<br \/>\n\t\t}<\/p>\n<p>\t\treturn sb.toString();<\/p>\n<p>\t}<\/p>\n<p>}[\/java]<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0cgetResponseCode() \u4f1a\u8fd4\u56de\u8fde\u63a5\u72b6\u6001\u7801( status code). \u8fd9\u662f\u4e00\u79cd\u83b7\u77e5\u989d\u5916\u7f51\u7edc\u8fde\u63a5\u4fe1\u606f\u7684\u6709\u6548\u65b9\u5f0f\u3002status code \u662f 200 \u5219\u610f\u5473\u7740\u8fde\u63a5\u6210\u529f.<\/p>\n<p>Convert the InputStream to a String(\u628aInputStream\u7684\u6570\u636e\u8f6c\u6362\u4e3aString)<br \/>\nInputStream \u662f\u4e00\u79cd\u53ef\u8bfb\u7684byte\u6570\u636e\u6e90\u3002\u5982\u679c\u4f60\u83b7\u5f97\u4e86\u4e00\u4e2a InputStream, \u901a\u5e38\u4f1a\u8fdb\u884cdecode\u6216\u8005\u8f6c\u6362\u4e3a\u5236\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u662f\u5728\u4e0b\u8f7d\u4e00\u5f20image\u6570\u636e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u50cf\u4e0b\u9762\u4e00\u4e0b\u8fdb\u884cdecode\uff1a<\/p>\n<p>[java]<br \/>\nInputStream is = null;<br \/>\n&#8230;<br \/>\nBitmap bitmap = BitmapFactory.decodeStream(is);<br \/>\nImageView imageView = (ImageView) findViewById(R.id.image_view);<br \/>\nimageView.setImageBitmap(bitmap);[\/java]<\/p>\n<p>\u5927\u591a\u6570\u8fde\u63a5\u7f51\u7edc\u7684Android app\u4f1a\u4f7f\u7528HttpURLConnection\u4f5c\u4e3a\u5ba2\u6237\u7aef\u6765\u53d1\u9001\u4e0e\u63a5\u53d7\u6570\u636e\u3002\u9664\u6b64\u4e4b\u5916\u8fd8\u53ef\u7528Apache HttpClient\u3002<\/p>\n<p>[java]HttpClient httpClient = new DefaultHttpClient();<br \/>\n\t\t\tHttpGet httpGet = new HttpGet(httpUrl);<br \/>\n\t\t\tHttpResponse response = httpClient.execute(httpGet);<\/p>\n<p>\t\t\tint statusCode = response.getStatusLine().getStatusCode();<br \/>\n\t\t\tLogs.v(&quot;statusCode &gt;&gt;&gt; :&quot; + statusCode);<\/p>\n<p>\t\t\tif (statusCode == HttpURLConnection.HTTP_OK) {<br \/>\n\t\t\t\tInputStream ins = response.getEntity().getContent();<\/p>\n<p>\t\t\t\tBufferedReader br = new BufferedReader(new InputStreamReader(ins,&quot;utf-8&quot;));<\/p>\n<p>\t\t\t\tStringBuilder sb = new StringBuilder();<br \/>\n\t\t\t\tString line = null;<br \/>\n\t\t\t\twhile ((line = br.readLine()) != null) {<br \/>\n\t\t\t\t\tsb.append(line);<br \/>\n\t\t\t\t}<\/p>\n<p>\t\t\t}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u5b66\u4e60Android\u7a0b\u5e8f\u5982\u4f55\u8fdb\u884c\u7f51\u7edc\u8fde\u63a5\uff0c\u83b7\u53d6\u7f51\u7edc\u8fd4\u56de\u6570\u636e\uff0c\u5982\u679c\u5bf9HTTP\u7f51\u7edc\u534f\u8bae\u8fd8\u4e0d\u4e86\u89e3\u7684\u53ef\u4ee5\u53c2\u8003\u6df1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/925"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=925"}],"version-history":[{"count":8,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":1731,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/925\/revisions\/1731"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}