[英]How to send the JSON response from JS file on request?
I have a JS file which generates a JSON object. 我有一个生成JSON对象的JS文件。
This JSON response string will be parsed in my android app after it is recievedfrom the URL of my JS file using this function. 这个JSON响应字符串将在我的Android应用程序中使用此函数从我的JS文件的URL收到后进行解析。
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
where url
is the URL of the JS file from which the JSON object has to be fetched. 其中
url
是必须从中获取JSON对象的JS文件的URL。
However, I can not figure out how to send the JSON response from my JS file. 但是,我无法弄清楚如何从我的JS文件发送JSON响应。
So ,once I have created the JSON object, how am I supposed to properly return that JSON reponse from my JS file so that it can be fetched? 所以,一旦我创建了JSON对象,我应该如何从我的JS文件中正确返回JSON响应,以便可以获取它?
EDIT : I am hosting it on Amazon Web Services, so I can install whatever Web Server softare is required to perform the task on my EC2 instance 编辑:我在亚马逊网络服务上托管它,所以我可以安装在我的EC2实例上执行任务所需的任何Web服务器软件
EDIT 2 The JS is basically the Google feed API returned in JSON result format ,which needs to be fetched in my android app 编辑2 JS基本上是以JSON结果格式返回的Google Feed API ,需要在我的Android应用程序中获取
I would always try to avoid JS on the server-side. 我总是试图在服务器端避免使用JS。 Of course you can run JS on your server with node.js for example, but I would only do this, if there is no other option.
当然,您可以使用node.js在服务器上运行JS,但是如果没有其他选项,我只会这样做。
So are you really sure you need JS on your server? 所以你真的确定你的服务器上需要JS吗? You can use the Google feed API in many other languages (take a look at this Google JSON guide ).
您可以使用许多其他语言的Google Feed API(请参阅此Google JSON指南 )。 You can directly access it in your Android Application (this is the Java-example from the Google JSON Guide, the android-code looks a bit different):
您可以在Android应用程序中直接访问它(这是Google JSON指南中的Java示例,android代码看起来有点不同):
URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
"v=1.0&q=Official%20Google%20Blog&userip=INSERT-USER-IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", /* Enter the URL of your site here */);
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
// now have some fun with the results...
Or you can do this with php, if you want to preprocess the API-response on your server: 或者,如果要在服务器上预处理API响应,可以使用php执行此操作:
$url = "https://ajax.googleapis.com/ajax/services/feed/find?" .
"v=1.0&q=Official%20Google%20Blog&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
You can use node.js or express.js to return response. 您可以使用node.js或express.js来返回响应。
Use JSON.stringify(objToJson)) you will get {"response":"value"} as response 使用JSON.stringify(objToJson))您将获得{“response”:“value”}作为响应
Sorry i am no expert in this area but you might want to look into these links. 对不起,我不是这个领域的专家,但你可能想看看这些链接。
Proper way to return JSON using node or Express 使用node或Express返回JSON的正确方法
Responding with a JSON object in NodeJS (converting object/array to JSON string) 在NodeJS中响应JSON对象(将对象/数组转换为JSON字符串)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.