Android向node.js编写的服务器发送数据并接收请求
作者:GISuuser 时间:2022-10-12 02:08:21
本文实例为大家分享了Android向node.js服务器发送数据并接收请求的具体代码,供大家参考,具体内容如下
首先时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);
}
});
}
}
最后是结果图:
来源:http://blog.csdn.net/gisuuser/article/details/77748779
标签:Android,node.js,服务器
0
投稿
猜你喜欢
详解SpringBoot项目整合Vue做一个完整的用户注册功能
2022-02-13 21:46:35
Spring框架开发scope作用域分析总结
2023-05-04 14:43:44
winform 中显示异步下载的图片
2022-09-03 19:04:07
Android Scroller实现弹性滑动效果
2022-12-08 11:01:02
JDK源码之PriorityQueue解析
2022-05-15 17:17:15
Java9的一些新特性介绍
2022-03-29 04:13:58
SpringMVC后端返回数据到前端代码示例
2023-06-20 13:12:47
Android studio点击跳转WebView详解
2022-12-12 05:11:17
Spring5中的WebClient使用方法详解
2023-08-05 14:50:24
Spring实战之SpEl语法实例详解
2023-09-18 07:56:03
List<>中Find的用法小结
2022-05-25 18:41:44
java实现简单的图书管理系统
2022-11-25 03:30:11
Java多线程之线程通信生产者消费者模式及等待唤醒机制代码详解
2022-10-31 11:17:22
java中LinkedList使用迭代器优化移除批量元素原理
2021-12-05 11:26:07
Android嵌套滚动NestedScroll的实现了解一下
2022-09-10 13:48:57
js判断是否含有GBK以外的字符的函数
2022-01-25 16:47:52
Mybatis SQL运行流程源码详解
2023-04-27 12:46:27
android 自定义控件 自定义属性详细介绍
2022-08-05 12:19:41
flyway实现java 自动升级SQL脚本的问题及解决方法
2021-10-14 00:23:19
springboot实现定时任务的四种方式小结
2021-10-20 20:38:06