提问者:小点点

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


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

使用此函数从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结果格式返回的Google feed API,它需要在我的android应用程序中获取


共2个答案

匿名用户

我总是尽量避免在服务器端使用JS。当然,您可以使用Node.JS在服务器上运行JS,但只有在没有其他选项的情况下,我才会这样做。

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

匿名用户

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

使用json.stringify(objToJson))您将获得{“response”:“value”}作为响应

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

使用node或Express返回JSON的正确方法

用NodeJS中的JSON对象响应(将对象/数组转换为JSON字符串)