Start a new topic

Ajax

Ajax


hi there!
 

This is a very specific ajax/server question and not related to our SDK.
I hope you understand that this forum targets Wikitude SDK related issues and questions.

Please have a look at our SDK sample app on how to convert JSON data into proper AR-Objects.
but check out e.g. stackoverflow to find answers on the client/server communication topic

http://stackoverflow.com/questions/4840727/read-write-json-from-js-to-file-on-server-not-server-app
http://stackoverflow.com/questions/9879895/client-server-communication-java-server

Best regards,
Andreas

 

Hi,

 

My problem is " idon't receive data JSON from my external server" to my android for showing POI after handling

in my file.js

i used request ajax// Using jQuery

$.ajax({
dataType: "json",
type: "POST",
url: "http://172.16.5.55:8080/AjaxParcelle/src/RequeteAjax.java",
data: {
lat:lat,
lon:lon
},
success: function(data, response)
{
console.log("test : " + data);

$.each(data, function(index, valeur) {
console.log(index, valeur);
});

World.loadPoisFromJsonData(data);
World.initiallyLoadedData = true;
},
error: function(err){
console.log('error : ' + err.statusText + ' ' + err.status);
}
});




 and in my server i created java code with servlette:

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Iterator;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

 

public class RequeteAjax extends HttpServlet {

 

private static final long serialVersionUID = 1L;

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request,response);

}

 

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {

 

double lat = Double.parseDouble(request.getParameter("lat"));

System.out.println(lat);

double lon = Double.parseDouble(request.getParameter("lon"));

PrintWriter out = response.getWriter();

response.setHeader("Content-Type", "application/json");

 response.setHeader("Access-Control-Allow-Origin", "*");

response.setHeader("Access-Control-Allow-Methods", "POST");

 response.setHeader("Access-Control-Allow-Headers", "Content-Type");

response.setHeader("Access-Control-Max-Age", "86400");

 

int Distance = 10000;

//Lecture fichier JSON

try

{

JSONParser parser = new JSONParser();

 

Object obj = parser.parse(new FileReader("D:\ProgrammeJEE\AjaxParcelle\WebContent\Data.json"));

 

JSONArray  jsonArray = new JSONArray();

JSONObject jsonObject = (JSONObject) obj;

 

//int id = (int) jsonObject.get("id");

//double longitude = (double) jsonObject.get("longitude");

//double latitude  = (double) jsonObject.get("latitude");

//String description  = (String) jsonObject.get("description");

//double altitude = (double) jsonObject.get("altitude");

//String name = (String) jsonObject.get("name");

 

JSONArray parameter = (JSONArray) jsonObject.get("parameter");

 

ArrayList al = new ArrayList();

 

for (int i=0; i<parameter.size(); i++)

{           

JSONObject objet = (JSONObject) parameter.get(i);

 

String name =  (String) objet.get("name");

 

String description = (String) objet.get("description");

 

Double latitude = (Double) objet.get("latitude");

 

Double longitude = (Double) objet.get("longitude");

 

Double altitude = (Double) objet.get("altitude");

 

Double id = (Double) objet.get("id");

 

Double distance2= 3959 * Math.acos(Math.sin(latitude)* Math.sin((lat))+ Math.cos(latitude)* Math.cos((lon))* Math.cos(longitude-(lon)));

 

if(distance2 < (double)Distance)

{  

al.add(objet);

System.out.println(al.toString());

//response.getWriter().println(al.toString());

//System.out.println(objet);

}

}

 

response.getWriter().println(al.toString());

}

catch(Exception ex)

{

System.out.println(ex.getMessage());

}

 

} catch (IOException e) {

e.printStackTrace();



}

 

 

 

}

 

 

 

Can you help me

thanks

Regard,

Mouna

 
Login or Signup to post a comment