【发布时间】: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