2016-03-05 60 views
3

我有一个生成JSON对象的JS文件。如何根据请求从JS文件发送JSON响应?

这个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响应,以便它可以被抓取?

编辑:我收留了它在亚马逊Web服务,这样我就可以安装任何Web服务器softare需要我的EC2实例执行任务

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

+0

你在哪里托管该js文件?你有一台运行NodeJs的服务器吗? Javascript是一种客户端脚本语言,没有关于你的服务器的知识,没有人能给你一个答案。 –

+0

我在亚马逊网络服务上托管它,所以我可以安装任何Web服务器软件需要在我的EC2实例上执行任务 –

+0

您能向我们展示您的Javascript的相关部分吗?如果它是网站的一部分,则显然不起作用。如果你真的想用JavaScript来编写你的服务器应用程序,你必须使用类似NodeJS的东西。 –

回答

1

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

所以你确定你的服务器上需要JS吗?您可以使用其他语言的Google提要API(请参阅Google JSON guide)。您可以直接访问它在你的Android应用程序(这是从谷歌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... 

或者你可以用PHP做到这一点,如果你想预处理服务器上的API响应:

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