【问题标题】:How to send the JSON response from JS file on request?如何根据请求从 JS 文件发送 JSON 响应?
【发布时间】:2016-06-19 04:05:52
【问题描述】:

我有一个生成 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 服务器软件

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

【问题讨论】:

  • 你在哪里托管那个 js 文件?你有运行 NodeJs 或其他东西的服务器吗? Javascript 是一种客户端脚本语言,如果不了解您的服务器,没有人可以给您答案。
  • 我将它托管在 Amazon Web Services 上,因此我可以安装在我的 EC2 实例上执行任务所需的任何 Web 服务器软件
  • 你能告诉我们你的 Javascript 的相关部分吗?如果它是网站的一部分,那么它显然不起作用。如果你真的想使用 javascript 来编写你的服务器应用程序,你必须使用 NodeJS 之类的东西。
  • JS基本上就是JSON result format中返回的Google feed API,需要在我的android app中获取
  • 试试jObj = new JSONObject(sb.toString());。它可能会起作用。

标签: javascript java android json parsing


【解决方案1】:

您可以使用 node.js 或 express.js 来返回响应。

使用 JSON.stringify(objToJson)) 你会得到 {"response":"value"} 作为响应

对不起,我不是这方面的专家,但您可能想查看这些链接。

Proper way to return JSON using node or Express

Responding with a JSON object in NodeJS (converting object/array to JSON string)

【讨论】:

    【解决方案2】:

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

    那么你真的确定你的服务器上需要 JS 吗?您可以在许多其他语言中使用 Google 提要 API(看看这个Google JSON guide)。您可以直接在您的 Android 应用程序中访问它(这是来自 Google JSON 指南的 Java 示例,android-code 看起来有点不同):

    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...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 2020-11-19
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2011-07-06
      • 2013-03-02
      相关资源
      最近更新 更多