×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Jun 2010
    Posts
    161

    Cross-Domain problem when accessing JSON (?)

    Hi,
    I'm trying to read some data from a remote Server. Namely todoist.com. I use the following code:

    Code:
        private String connect(String targetUrl) 
    	{
    		String response="";
    		HttpConnection connection = null;
    		InputStream inputstream = null;
    	    try
    	    {
    	    	connection = (HttpConnection) Connector.open(targetUrl);
    	        //HTTP Request
    	        connection.setRequestMethod(HttpConnection.GET);
    	        //connection.setRequestProperty("Content-Type","text/xml");
    	        connection.setRequestProperty("Connection", "close");
    	        // HTTP Response
    	        if (connection.getResponseCode() == HttpConnection.HTTP_OK)
    	        {	          
    	        	inputstream = connection.openInputStream();
    	        	int length = (int) connection.getLength();
    	        	if (length != -1)
    	        	{
    	        		byte incomingData[] = new byte[length];
    	        		inputstream.read(incomingData);
    	        		response = new String(incomingData);
    	        	}
    	        	else  
    	        	{
    	        		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    	        		int ch;
    	        		while ((ch = inputstream.read()) != -1)
    	        		{
    	        			bytestream.write(ch);
    	        		}
    	        		response = new String(bytestream.toByteArray());
    	        		bytestream.close();	            
    	        	}	          
    	        }
    	    }	
    	    catch(IOException error)
    	    {
    	             System.out.println(error.toString());
    	    }
    	    finally
    	    {
    	    	if (inputstream!= null)
    	        {
    	          try 
    	          { 
    	            inputstream.close();
    	          }
    	          catch( Exception error)
    	          {
    	             /*log error*/
                          System.out.println(error.toString());
    	          }
    	        }
    	        if (connection != null)
    	        {
    	          try
    	          {
    	             connection.close();
    	          }
    	          catch( Exception error)
    	          {
    	             /*log error*/
                          System.out.println(error.toString());
    	          }
    	        }
    	    }
                System.out.println("H: " + response);
    	    return response;		
    	}
    When I try to get the data from the Standard API via
    Code:
    connect("https://todoist.com/API/getProjects?token=MYAPITOKEN");
    everything works exactly like expected and I get the JSON data back. But when I use
    Code:
    connect("https://todoist.com/TodoistSync/get?apitoken=MYAPITOKEN");
    I only get a empty answer. Without any error. Writing the same URL directly into the browser give me the data, so the URL is correct. My guess is a Cross-Domain policy problem but maybe someone here has a solution for me?

    Thx

  2. #2
    Nokia Developer Expert
    Join Date
    Oct 2007
    Posts
    441

    Re: Cross-Domain problem when accessing JSON (?)

    Hi

    It does not look like cross-domain request problem because that problem refers to AJAX use with JavaScript. The point is browser cannot allow you make request to other domain than you are working with for security reason. However JavaScript can be fetched from different domain and executed then -- that is one of the work-around.

    And more, as you have pointed in you post , in fact you are making request in the same domain "todoist.com" but using different url.

    If you can use your web service via plain http (not https), i suggest you to investigate problem with a browser and a debugging tool to capture http traffic -- for example FireFox and FireBug.

    If plain http is not possible -- you have to use openssl to decode https traffic ---- anyway it seems there is your web service API problem --- check the API documentation : maybe you use it incorrectly

  3. #3
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Cross-Domain problem when accessing JSON (?)

    Hi lorion84,

    Java ME is very sensitive when it comes to the Content-Types via a GET request, unlike the browser where you don't need to know and define the content type of the request. It seems to me, that your issue is related to the fact that you haven't changed the content type of your JSON request to one of these:

    application/json
    text/javascript
    text/json
    text/x-json

    with the first one being most likely the correct. As izinin suggested, you can always perform the get request via a tool such as Fiddler that displays the header information of the request/response including the Content-type. You can then copy paste the content-type you receive to your code and remove the comments from this line:

    Code:
    //connection.setRequestProperty("Content-Type","text/xml");

Similar Threads

  1. Json problem on device
    By junw in forum Symbian
    Replies: 1
    Last Post: 2011-01-21, 05:26
  2. Problem in Resolving Domain Name
    By swarupk2007 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2010-02-02, 11:53
  3. Domain Name problem
    By dit6 in forum Mobile Java General
    Replies: 14
    Last Post: 2009-06-11, 11:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×