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

Javascript 如何根据请求从JS文件发送JSON响应?,javascript,java,android,json,parsing,Javascript,Java,Android,Json,Parsing,我有一个生成JSON对象的JS文件 此JSON响应字符串将在我的android应用程序中使用此函数从我的JS文件的URL接收后进行解析 public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient();

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

此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;
 }
其中
url
是必须从中提取JSON对象的JS文件的url

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

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


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


编辑2JS基本上是返回的,需要在我的android应用程序中获取

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

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

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


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

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

如果要在服务器上预处理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...

你在哪里托管js文件?你有运行NodeJs的服务器吗?Javascript是一种客户端脚本语言,在不了解您的服务器的情况下,没有人能给您答案。我将它托管在Amazon Web Services上,因此我可以安装在我的EC2实例上执行任务所需的任何Web服务器软件。您能向我们展示Javascript的相关部分吗?如果它是一个网站的一部分,那么它显然不会工作。如果你真的想用javascript来编写你的服务器应用程序,你必须使用类似NodeJS的东西。JS基本上是返回的,需要在我的android apptry
jObj=new JSONObject(sb.toString())中获取。这可能有用。
$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...