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

标签 javascript java android json parsing

我有一个生成 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/

相关文章:

java - Android Lollipop Material Design 溢出菜单图标颜色

android - 只读文件系统android(是/mnt不是/system)

android - Android 5 Lollipop 中的通知栏图标变为白色

javascript - 如何自动将文件拖放到 headless 浏览器的窗口中?

java - 在 ExecutorService 中 hibernate 一个线程 (Java/Clojure)

java - 将 Apache Orc 文件列名与列统计信息相匹配

android - editText 和软键盘之间的空间

javascript - Angular : How to use variable in HTML getElementById

javascript - 如何为放大的移动设备居中模态

c# - Javascript getElementbyId 工作一次但不能工作两次?