Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù)并接收請求
本文實(shí)例為大家分享了Android向node.js服務(wù)器發(fā)送數(shù)據(jù)并接收請求的具體代碼,供大家參考,具體內(nèi)容如下
首先時node.js服務(wù)器端代碼
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事件觸發(fā)后,通過querystring.parse將post解析為真正的POST請求格式,然后向客戶端返回。 request.on('end', function(){ post = queryString.parse(post); console.log("請求結(jié)束"+post.body); response.writeHead(200, {"Content-Type": "text/plain;charset=utf8"}); response.write("請求成功"); response.end(); }); }).listen(8888); console.log("服務(wù)器啟動"); function writeFile(str) { fs.writeFile('E:/log.txt', str, function (err) { if (err) { return console.error(err); } console.log("數(shù)據(jù)寫入成功!"); }) }
然后是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();//響應(yīng) } 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); } }); } }
最后是結(jié)果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android中TabHost的圖標(biāo)(48×48)和文字疊加解決方法
開發(fā)過程中,有時候圖標(biāo)稍微大點(diǎn),比如48×48的時候,文字就會和圖標(biāo)疊加起來,遇到這種問題我們該怎樣處理呢?本文將詳細(xì)介紹希望對你有所幫助2013-01-01Android自定義Notification添加點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android自定義Notification添加點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解
這篇文章主要介紹了Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08AndroidGUI27中findViewById返回null的快速解決辦法
這篇文章主要介紹了AndroidGUI27中findViewById返回null的快速解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Android?RxJava與Retrofit結(jié)合使用詳解
RxJava和Retrofit的結(jié)合使用估計(jì)已經(jīng)相當(dāng)普遍了,自己工作中也是一直都在使用。在使用的過程中我們都會對其進(jìn)行封裝使用,GitHub上也有很多封裝好的項(xiàng)目可以直接拿來使用,其實(shí)對于開源框架的二次封裝有時候針對不同的業(yè)務(wù)邏輯封裝的過程中也多多少少有些不同2023-03-03