本文实例为大家分享了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); } }); } }
最后是结果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android向node.js编写的服务器发送数据并接收请求全部内容,希望文章能够帮你解决Android向node.js编写的服务器发送数据并接收请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)