gpt4 book ai didi

javascript - 如何根据请求从 JS 文件发送 JSON 响应?

转载 作者:太空狗 更新时间:2023-10-29 14:00:41 25 4
gpt4 key购买 nike

我有一个生成 JSON 对象的 JS 文件。

使用此函数从我的 JS 文件的 URL 接收到此 JSON 响应字符串后,将在我的 android 应用程序中对其进行解析。

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;
}

其中 url 是必须从中获取 JSON 对象的 JS 文件的 URL。

但是,我不知道如何从我的 JS 文件发送 JSON 响应。

那么,一旦我创建了 JSON 对象,我应该如何从我的 JS 文件中正确返回该 JSON 响应以便可以获取它?

编辑:我将其托管在 Amazon Web Services 上,因此我可以安装在我的 EC2 实例上执行任务所需的任何 Web 服务器软件

EDIT 2 JS 基本上是 Google feed API返回 JSON result format ,需要在我的 android 应用程序中获取

最佳答案

我总是尽量避免在服务器端使用 JS。当然,您可以使用 node.js 在您的服务器上运行 JS,但如果没有其他选择,我只会这样做。

那么你真的确定你的服务器需要 JS 吗?您可以使用许多其他语言的 Google 提要 API(看看这个 Google JSON guide )。您可以在您的 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...

或者如果你想在你的服务器上预处理 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...

关于javascript - 如何根据请求从 JS 文件发送 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com