Java 使用Tianium将JSON发布到web服务
嗨,我试着跟着问题的答案走 我试图将一个JSON对象发布到一个web服务,该web服务是一个正在运行的Javaservlet 我试着发布到/Test/retrieveData,方法名称如上面的答案所示,但似乎没有发送 我对servlets和titanium仍然很陌生,我的servlet能够处理这个post请求吗,还是我需要创建一个新的servlet? 谢谢你的帮助 钛片 }) Javaservlet 编辑Java 使用Tianium将JSON发布到web服务,java,json,apache,web-services,titanium-mobile,Java,Json,Apache,Web Services,Titanium Mobile,嗨,我试着跟着问题的答案走 我试图将一个JSON对象发布到一个web服务,该web服务是一个正在运行的Javaservlet 我试着发布到/Test/retrieveData,方法名称如上面的答案所示,但似乎没有发送 我对servlets和titanium仍然很陌生,我的servlet能够处理这个post请求吗,还是我需要创建一个新的servlet? 谢谢你的帮助 钛片 }) Javaservlet 编辑 你能在OneError console.loge.error+-+this.status中
你能在OneError console.loge.error+-+this.status中添加这个吗昨天,我在Java Servlet中添加了一个doPost函数,并且不再有错误消息,JSON正在发送,我只是不知道现在如何在Servlet页面上显示它。谢谢你的评论Tony,你现在知道我如何在servlet页面上显示JSON对象了吗?我是sry,我不知道,但我认为你只需要将JSON对象转换为java对象,获取它的值,然后在页面中按照你的需要格式化它
button.addEventListener('click', function(e) {
Ti.API.info('Button Pressed');
var params = {
Country : textCountry.value,
Capital : textCapital.value
};
var xhr = Ti.Network.createHTTPClient({});
xhr.setTimeout(10000);
//set enconding
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
// function to deal with errors
xhr.onerror = function() {
Ti.API.info('error, HTTP status = ' + this.status);
alert('Error Sending Data');
};
// function to deal with response
xhr.onload = function() {
var obj = JSON.parse(this.responseText);
};
xhr.open("POST", 'http://130.206.127.43:8080/Test/retrieveData');
xhr.send(JSON.stringify(params));
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
JSONObject mainObj = retrieveData();
response.setContentType("application/json");
String output = mainObj.toString();
PrintWriter writer = response.getWriter();
writer.write(output);
writer.close();
}
private JSONObject retrieveData()
{
JSONObject json = new JSONObject();
JSONArray ja = new JSONArray();
json.put("Country", "Ireland");
json.put("Capital", "Dublin");
ja.put(json);
json = new JSONObject();
json.put("Country", "Spain");
json.put("Capital", "Madrid");
ja.put(json);
json = new JSONObject();
json.put("Country","France");
json.put("Capital", "Paris");
ja.put(json);
json = new JSONObject();
JSONObject mainObj = new JSONObject();
mainObj.put("places", ja);
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
JSONObject mainObj = retrieveData();
response.setContentType("application/json");
String output = mainObj.toString();
PrintWriter writer = response.getWriter();
writer.write(output);
writer.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
private JSONObject retrieveData()
{
JSONObject json = new JSONObject();
JSONArray ja = new JSONArray();
json.put("Country", "Ireland");
json.put("Capital", "Dublin");
ja.put(json);
json = new JSONObject();
json.put("Country", "Spain");
json.put("Capital", "Madrid");
ja.put(json);
json = new JSONObject();
json.put("Country","France");
json.put("Capital", "Paris");
ja.put(json);
json = new JSONObject();
JSONObject mainObj = new JSONObject();
mainObj.put("places", ja);
return mainOBj;
}