欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù)并接收請求

 更新時間:2020年07月29日 10:50:13   作者:GISuuser  
這篇文章主要為大家詳細(xì)介紹了Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù),并接收請求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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手機(jī)鬧鐘用法實(shí)例

    Android手機(jī)鬧鐘用法實(shí)例

    這篇文章主要介紹了Android手機(jī)鬧鐘用法,以實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)鬧鐘功能的頁面布局及具體功能相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • android中TabHost的圖標(biāo)(48×48)和文字疊加解決方法

    android中TabHost的圖標(biāo)(48×48)和文字疊加解決方法

    開發(fā)過程中,有時候圖標(biāo)稍微大點(diǎn),比如48×48的時候,文字就會和圖標(biāo)疊加起來,遇到這種問題我們該怎樣處理呢?本文將詳細(xì)介紹希望對你有所幫助
    2013-01-01
  • Android ADB簡介、安裝及使用詳解

    Android ADB簡介、安裝及使用詳解

    ADB 全稱為 Android Debug Bridge,起到調(diào)試橋的作用,是一個客戶端-服務(wù)器端程序,其中客戶端是用來操作的電腦,服務(wù)端是 Android 設(shè)備,這篇文章介紹Android ADB簡介、安裝及使用,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Android zip文件下載和解壓實(shí)例

    Android zip文件下載和解壓實(shí)例

    這篇文章主要介紹了Android zip文件下載和解壓實(shí)例,有需要的朋友可以參考一下
    2014-01-01
  • Android自定義Notification添加點(diǎn)擊事件

    Android自定義Notification添加點(diǎn)擊事件

    這篇文章主要為大家詳細(xì)介紹了Android自定義Notification添加點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解

    Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解

    這篇文章主要介紹了Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • AndroidGUI27中findViewById返回null的快速解決辦法

    AndroidGUI27中findViewById返回null的快速解決辦法

    這篇文章主要介紹了AndroidGUI27中findViewById返回null的快速解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android解決View的滑動沖突的方法

    Android解決View的滑動沖突的方法

    這篇文章主要介紹了Android解決View的滑動沖突的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Android?RxJava與Retrofit結(jié)合使用詳解

    Android?RxJava與Retrofit結(jié)合使用詳解

    RxJava和Retrofit的結(jié)合使用估計(jì)已經(jīng)相當(dāng)普遍了,自己工作中也是一直都在使用。在使用的過程中我們都會對其進(jìn)行封裝使用,GitHub上也有很多封裝好的項(xiàng)目可以直接拿來使用,其實(shí)對于開源框架的二次封裝有時候針對不同的業(yè)務(wù)邏輯封裝的過程中也多多少少有些不同
    2023-03-03
  • Android入門之使用OKHttp多線程下載文件

    Android入門之使用OKHttp多線程下載文件

    OkHttp是一個神器。OkHttp分為異步、同步兩種調(diào)用。今天我們就會基于OkHttp的異步調(diào)用實(shí)現(xiàn)一個多線程并行下載文件并以進(jìn)度條展示總進(jìn)度的實(shí)用例子,需要的可以參考一下
    2023-01-01

最新評論