您好,登錄后才能下訂單哦!
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下
首先時node.js服務器端代碼
var http = require("http"); var fs = require("fs"); var queryString = require('querystring'); var url = require('url'); var util = require('util'); http.createServer(function (request, response) { // 定義了一個post變量,用于暫存請求體的信息 var post = ''; request.on('data', function(chunk){ post += chunk; }); // 在end事件觸發后,通過querystring.parse將post解析為真正的POST請求格式,然后向客戶端返回。 request.on('end', function(){ post = queryString.parse(post); console.log("請求結束"+post.body); response.writeHead(200, {"Content-Type": "text/plain;charset=utf8"}); response.write("請求成功"); response.end(); }); }).listen(8888); console.log("服務器啟動"); function writeFile(str) { fs.writeFile('E:/log.txt', str, function (err) { if (err) { return console.error(err); } console.log("數據寫入成功!"); }) }
然后是Android部分
package com.example.hanbo.servertest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.json.JSONObject; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.URLEncoder; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView= (TextView) findViewById(R.id.textView); Button button= (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText("開始請求"); new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; URL url = null; try { url = new URL("http://192.168.1.177:8888"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.setRequestProperty("Content-Type","application/json"); OutputStream outputStream=connection.getOutputStream(); BufferedWriter requestBody=new BufferedWriter(new OutputStreamWriter(outputStream)); String str = URLEncoder.encode("抓哇", "UTF-8"); requestBody.write("name=javaPost&body=1"); requestBody.flush(); requestBody.close(); getResponseJava(connection); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } private void getResponseJava(HttpURLConnection urlConnection) { InputStream in = null; try { in = urlConnection.getInputStream();//響應 } catch (IOException e) { urlConnection.disconnect(); //textView.setText(e.getMessage()); return; } BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } final StringBuilder result = new StringBuilder(); String tmp = null; try { while((tmp = reader.readLine()) != null){ result.append(tmp); } } catch (IOException e) { //textView.setText(e.getMessage()); return; } finally { try { reader.close(); urlConnection.disconnect(); } catch (IOException e) { } } runOnUiThread(new Runnable() { @Override public void run() { textView.setText(result); } }); } }
最后是結果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。