WWW错误:Java.net.protocol异常:超出内容长度限制

时间:2015-12-11 15:55:59

标签: java c# android unity3d

我正在构建Unity应用程序。其中一部分要求我将图像从移动设备发送到服务器。代码在笔记本电脑上工作正常,但在移动设备中它会抛出错误:

  

WWW错误:Java.net.protocol异常:超出内容长度限制。

捕获和发送图像的脚本附在下面。

与发送图片有关的方法是sendimage(byte[])WaitForRequest(WWW)

using UnityEngine;
using System.Collections;
using System;
using System.IO; 
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.IO;


public class imageClient : MonoBehaviour
{
   public string deviceName;
   WebCamTexture wct;
   public string path = "";
   public string pth;
   public string responseAsString;
   byte[] data;
   Texture2D snap;
   public string response = "Request not made";

   void Start()
   {
      Debug.Log("IT has Started");
      WebCamDevice[] devices = WebCamTexture.devices;     //detecting all webcam devices
      deviceName = devices[0].name;
      wct = new WebCamTexture();                          //setting cubes texture as the webcam view and taking appropriate snapshot
      wct.deviceName = deviceName;
      wct.requestedHeight = 400;
      wct.requestedWidth = 300;
      wct.requestedFPS = 20;
      GetComponent<Renderer>().material.mainTexture = wct;   
   }

   void OnGUI()
   {
      path = Application.persistentDataPath;              //path of the apk
      if (GUI.Button(new Rect(10, 70, 650, 120), response))
      {
         wct.Play();
         StartCoroutine(TakeSnapshot());                 //snapshot taken and sent as POST request to the server
      }
   }

   private IEnumerator TakeSnapshot()
   {
      yield return new WaitForEndOfFrame();               //wait for the frame to get properly captured
      Debug.Log("0");
      Texture2D snap = new Texture2D(wct.width, wct.height);
      Debug.Log("1");
      snap.SetPixels(wct.GetPixels());
      Debug.Log("2");
      snap.Apply();
      Debug.Log("3");

      byte[] data = snap.EncodeToPNG();                   //frame image stored as png
      System.IO.File.WriteAllBytes(path + "/asimunmesh.png", data);
      sendimage(data);
      Debug.Log("8");
   }

   public WWW sendimage(byte[] data)
   {
      const string url = "http://posttestserver.com/post.php";  //put in the server url to which to post request 

      Debug.Log("Entering request method");

      //byte[] postData = File.ReadAllBytes(flacName);
      //Debug.Log("Read flac file. Size: " + data.Length + " bytes");

      var form = new WWWForm();
      var headers = form.headers;

      headers["Method"] = "POST";
      headers["Content-Type"] = "image/png";
      headers["Content-Length"] = data.Length.ToString();
      // headers["Accept"] = "application/json";

      form.AddBinaryData("fileUpload", data, "pngFile", "image/png");
      var httpRequest = new WWW(url, form.data, headers);
      Debug.Log("waiting for reply back");
      StartCoroutine(WaitForRequest(httpRequest));
      return httpRequest;
   }

   private IEnumerator<WWW> WaitForRequest(WWW www)
   {
      yield return www;

      // check for errors
      if (www.error == null)
      {
         Debug.Log("WWW Ok!: " + www.text);
         response = "WWW Ok!: " + www.text;
      }
      else
      {
         Debug.Log("WWW Error: " + www.error);
         response = "WWW Error: " + www.error;
      }
   }
}

我认为android上的java servlet设置了一些限制,它可以发送多少数据。 但我怀疑这可能是错误的原因。

更新:我试图发送一个2字节的简单字符串。它也引发了同样的异常:协议异常:超出2个字节的内容长度限制

1 个答案:

答案 0 :(得分:0)

这不是问题的解决方案。但我发现使用UnityEngine.WWW类在android中有一些问题。这个问题可能与班级本身有关。无论如何,Github上有一个开源Unity插件可以帮助你在iOS和OSX上完成这个(发出HTTP请求)。

https://github.com/imkira/unity-urlclient

这是存储库的链接,您可以找到该插件。 我在Android上测试了它,它似乎工作正常。