0

I have created a basic Java Servlet page which just displays hello world

What im trying to learn and understand is how to basically pull the information down from a web server and display it on the mobile device (and then hopefully post information back up to the web page)

I have some sample code here, when the application runs there is no connection error everything goes through as planned, the alert message displays Hello World which im guessing is the 'this.response.text)

I have been reading through the documentation given from Appcelerator Titanium but find it hard to understand with the JSON files and parsing etc..

Q. so if anyone can help me understand how i can pull 'Hello World'' down from the servlet page and display maybe in a label/textfield thanks hopefully your answers will then help me understand how to take data from the mobile client and send to information to the web page

var xhr = Ti.Network.createHTTPClient({
onload : function(e){
    Ti.API.info('Received text: ' + this.responseText);
    alert(this.responseText);


},
onerror: function(e) {
    Ti.API.info('error, HTTP status = ' + this.status);
    alert('error');
},
timeout:5000
});

xhr.open("GET", "http://130.206.127.43:8080/HelloWorld");
xhr.send();
Joshua Oneill
  • 33
  • 1
  • 6

3 Answers3

0

I think what you want is to return the results from your Java servlet as a JSON string so that you can read it properly in Titanium.

Start by creating a Map or w/e with the information you want to return to the client.

{message=Hello world, action=display, extra=extra}

Then convert it to a JSON string (examples)

Now in your Titanium application you can parse the result as JSON and use your returned info:

onload : function(e){
    var result = JSON.parse(this.responseText);
    alert(result.message);
    alert(result.action);
    alert(result.extra);
},

EDIT: Replaced the eval

Jeroen
  • 1,991
  • 2
  • 16
  • 32
  • [eval is evil](http://stackoverflow.com/questions/646597/eval-is-evil-so-what-should-i-use-instead). – turtle Jan 13 '15 at 05:40
0

I think it will not respond to since you try to pull json form which not exist in your servlet page

what you need to work with you is a web service page contain data in a JSON form like :

[{"message" : "Hello World"}]

so if you have good knowledge in j2ee try to write web service page contain json form

then inside onload function put this :

var json = eval('('+this.responseText+')');

and show the data using alert(json.message);

0

thank you for your answers, this is the basic servlet class

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException
{

  message = "Hello World";
}

public void doGet(HttpServletRequest request,
                HttpServletResponse response)
        throws ServletException, IOException
{
  // Set response content type
  response.setContentType("text/html");

  // Actual logic goes here.
  PrintWriter out = response.getWriter();
  out.println("<h1>" + message + "</h1>");
}

unfortunately i do not have good knowledge of j2ee but i will look this up now but would i need to change

public void init() throws ServletException
{

  message = "Hello World";
}
...to
public void init() throws ServletException
{

  [{"message" : "Hello World"}]
}

as well as

response.setContentType("text/html");
...to
response.setContentType("application/json");
Joshua Oneill
  • 33
  • 1
  • 6
  • Please edit your question instead of answering it with another question. But what you are asking is true, you need to change that although '[{"message" : "Hello World"}]' needs to be a string so output = '[{"message" : "Hello World"}]'. But as i said in my answer you can convert Map to a JSOn string. – Jeroen Jan 13 '15 at 09:44
  • will do in future new to stack, thanks for your help – Joshua Oneill Jan 13 '15 at 09:52
  • all right you might want to accept an answer if answers your question. – Jeroen Jan 13 '15 at 11:30