您的位置:首页 > 理论基础 > 计算机网络

unity网络编程学习(3)与javaWeb的http通信

2014-12-24 09:09 453 查看

前言

unity想要实现和网页之间传递数据的功能,就要用到Unity中的WWW类,它是一个简单的访问网页的类,用于检索URL内容的小工具模块。如果你想从web服务器上获取一些数据,例如高分列表或者调用主页,可以使用这个,也有一些功能可以使用从web上下载的图片来创建一个纹理,或者下载或加载新的web播放器数据文件。WWW类可以用来发送GET和POST请求到服务器,WWW类默认使用GET方法,并且如果提供一个postData参数可用POST方法。

让我们来看一下它的构造参数吧

static function WWW (url :
string, postData : byte[], headers : Hashtable)
: WWW


Parameters参数

url
The url to download.
请求的网址
postData
A byte array of data to be posted to the url.
数据post到url的一个字节数组
headers
A hash table of custom headers to send with the request. 第三个参数,可以省略

看看效果





请求得到了回复,图片也下载下来了,

实现

服务器的搭建,通过eclipse来创建一个web工程,创建过程就不演示了,创建一个helloworld,开启服务器,看看是否正常运行


看来没问题

之后我们需要创建一个servlet文件,来处理来自unity的请求和回复内容
package com.zx.uinty;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public Servlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doget");
this.excute(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("dopost");
this.excute(request, response);
}

private void excute(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取参数
String id = request.getParameter("id");
String name = request.getParameter("name");
System.out.println("id是: "+id+" name是: "+name);
//设置文档类型为text
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("服务器返回");
out.println("访问方式为: "+request.getMethod()+"\n"+"得到的参数是:id "+id+", name "+name);
}

public void destroy() {
}

}
将一张图片放入到WebContent文件夹下,用来测试www类下载图片的功能,这样服务器端需要的内容就已经完成了,保证服务的开启。
通过Ngui来搭建一个简单的场景。
创建一个c#脚本,挂在MainCamera上作为控制脚本
给按钮添加方法,点击时触发
using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

public UIInput id, name,text;
public UITexture Pic;

/*点击提交按钮,发送请求,传递参数*/
public void OnSubmit()
{
Debug.Log("进入OnSubmit方法");
StartCoroutine(TestPost());
}
/*点击下载图片按钮,下载图片*/
public void OnDownPic()
{
StartCoroutine(TestDown());
}
//在C#中, 需要用到yield的话, 必须建立在IEnumerator类中执行
IEnumerator TestPost()
{
//WWW的三个参数: url, postData, headers
string url = "http://127.0.0.1:8000/WebDemo1/Servlet";
byte[] post_data;
Hashtable headers;   //System.Collections.Hashtable
string str_params;
str_params = "id=" + id.value + "&" + "name=" + name.value;
//设置编码格式为utf-8
post_data = System.Text.UTF8Encoding.UTF8.GetBytes(str_params);
headers = new Hashtable();
headers.Add("CONTENT_TYPE", "text/plain");
//发送请求
WWW www_instance = new WWW(url, post_data, headers);
//web服务器返回
yield return www_instance;
if (www_instance.error != null)
{
Debug.Log(www_instance.error);
}
else
{   //显示返回数据
text.value = www_instance.text;
}
}

IEnumerator TestDown() {
Debug.Log("进入www方法");
string url = "http://127.0.0.1:8000/WebDemo1/Pic.jpg";
//发送请求
WWW www_instance = new WWW(url);
//web服务器返回
yield return www_instance;
if (www_instance.error != null)
{
Debug.Log(www_instance.error);
}
else
{   //显示下载的图片
Pic.mainTexture = www_instance.texture;
}
}
}

总结

通过unity中www类,就可以和后台服务器进行数据的交互,可以达到数据之间相互的传输,可以应用到网页游戏等一些联网游戏,也可以完成登陆功能:访问服务器再传递到数据库,做出判断再向上逐级返回,完成一个完整的流程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: