0

I want to get a string variable (called level) from a servlet n C# (Unity script). For build reasons I call the servlet using www.

The code in my C# script is:

public void setLevel()
{
    string url = "http://localhost:8080/my_project/servlet";
    WWW www = new WWW(url);
    StartCoroutine(WaitForRequestLevel(www));
}

public IEnumerator WaitForRequestLevel(WWW www)
{
    yield return www;
    // check for errors
    if ((www.error == null) && (www.isDone))
    {
        level = int.Parse(www.text);

        Debug.Log("Setting Ok!: " + www.text);
    }
    else
    {
        Debug.Log("Setting Error: " + www.error);
    }
}

The code of my servlet is:

public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException 
{
    String var="2";
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter writer = response.getWriter();
    writer.write(Integer.toString(var);
}

All I want is that the level variable would be equal to 2, but I always get 500 status error and I guess it's from the response.setContentType.

Can anyone show me how to solve my problem?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Soufien Hajji
  • 477
  • 1
  • 8
  • 24
  • If the error originates on the server, shouldn't the server be able to tell you what's wrong? 500 is a server error. I would expect the server to return 400 if the request was malformed. – cwharris Mar 16 '18 at 20:01
  • Are you sure your code compiles? I don't believe Integer.toString() takes a String parameter. – Olaf Mar 16 '18 at 20:10
  • @Olaf yes it was a mistake i ommited a huge part of the code to make it easier to understant – Soufien Hajji Mar 16 '18 at 21:15
  • @ChristopherHarris the error is shown in the unity console but i don't know what that means – Soufien Hajji Mar 16 '18 at 21:15
  • Soufien, you're running the server locally. Look at the server logs to figure out what's going on. 500 means the server failed to process the request appropriately. It may not even be an issue with your unity code. – cwharris Mar 16 '18 at 21:22

0 Answers0