Android Java Google Places GET (Pull) Reviews JSONException 评论没有价值
Android Java Google Places GET (Pull) Reviews JSONException No value for reviews
我检查并确保 android 的 google 个位置 api 已启用。
起初我用的是这个url:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=34.0467359,-118.441764&radius=1000&sensor=true&key=YOUR_PLACE_API_KEY
然后切换到这个:
https://maps.googleapis.com/maps/api/place/details/json? reference=REFERENCE_STRING_OF_THE_PLACE&sensor=true&key=YOUR_PLACE_API_KEY
根据
中的建议
Android - Google Places API - No "reviews" Array
但仍然出现同样的错误。
在 https://developers.google.com/places/web-service/details
据说引用已被弃用,现在我们必须使用 place_id。我将 url 更改为:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=YOUR_API_KEY
后来我在 Whosebug 上读到,2012 年 google 地方 API 没有提取评论的功能。这适用于现在吗? 2016年?如果没有,请帮助我在下面的代码中破译我做(正在做)错的地方。
如果我复制并粘贴上述任何 url 的任何日志输出,评论会显示在我的浏览器中,但响应中的输出不会显示 url。
我收到这个错误:
W/System.err: org.json.JSONException: No value for reviews
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
W/System.err: at DownloadReviewsTask.doInBackground(DownloadReviewsTask.java:69)
W/System.err: at DownloadReviewsTask.doInBackground(DownloadReviewsTask.java:19)
W/System.err: at android.os.AsyncTask.call(AsyncTask.java:292)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
下面是我的代码:
public class JSONParser {
String charset = "UTF-8";
HttpURLConnection conn;
DataOutputStream wr;
StringBuilder result = new StringBuilder();
URL urlObj;
JSONObject jObj = null;
StringBuilder sbParams;
String paramsString;
public JSONObject makeHttpRequest(String url, String method,
HashMap<String, String> params) {
sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
if (method.equals("POST")) {
// request method is POST
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", charset);
conn.setReadTimeout(90000);
conn.setConnectTimeout(90000);
conn.connect();
paramsString = sbParams.toString();
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(90000);
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
// try parse the string to a JSON object
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
}
public class DownloadReviewsTask extends AsyncTask<String, Void, HashMap<String, List<MyModel>>> {
//Declarations
private Context mContext;
public Exception mException;
private OnEventListener<HashMap<String, List<MyModel>>> mCallBack;
JSONArray reviews;
String author_name;
String text;
//Constructor
public DownloadReviewsTask(Context context, OnEventListener callback) {
mCallBack = callback;
mContext = context;
}
//sets and passes the review author and text for each review and sets them in the My Model
public MyModel setPlace(JSONObject thisObject) throws JSONException {
JSONObject results = thisObject.getJSONObject("results");
MyModel thisMyModel = new MyModel();
if (!results.isNull("reviews")) {
reviews = results.getJSONArray("reviews");
Log.e("reviews array setPlace", String.valueOf(reviews));
for (int i = 0; i < reviews.length(); i++) {
//if(i==reviews.length()) break;
// if (thisObject.getJSONArray("reviews").getJSONObject(i).has("author_name")) //{
author_name = results.getJSONArray("reviews").getJSONObject(i).getString("author_name");//reviews.getJSONObject(i).getString("author");
Log.e("review_author in setup", results.getJSONArray("reviews").getJSONObject(i).getString("author_name"));
// }
//if (thisObject.getJSONArray("reviews").getJSONObject(i).has("text")) {
Log.e("review text in setup", results.getJSONArray("reviews").getJSONObject(i).getString("text"));
text = results.getJSONArray("reviews").getJSONObject(i).getString("text");//}
thisMyModel.setAuthor_name(author_name);
thisMyModel.setText(text);
}
}
return thisMyModel;
}
@Override
protected HashMap<String, List<MyModel>> doInBackground(String... args) {
try {
JSONParser jsonParser = new JSONParser();
JSONObject json;
String reviewUrl = args[0];
Log.e("reviewUrl",reviewUrl);
//Use a HashMap instead with the varargs:
HashMap<String, String> params = new HashMap<>();
json = jsonParser.makeHttpRequest(reviewUrl, "GET", params);
HashMap<String, List<MyModel>> myResults = new HashMap<>();
if(json != null) {
Log.e("reviews result", json.toString());
JSONArray parentArray = json.getJSONArray("reviews");
List<MyModel> allEventsList = new ArrayList<>();
//loops through json array
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
allEventsList.add(setPlace(finalObject));
Log.e("allEventsList", String.valueOf(allEventsList));
}
myResults.put("reviews", allEventsList);
Log.e("myResults",String.valueOf(myResults));
return myResults;
}else{
return null;
}
}
catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, List<MyModel>> result) {
if (mCallBack != null) {
if (mException == null) {
mCallBack.onSuccess(result);
} else {
mCallBack.onFailure(mException);
}
}
}
}
public class MyModel{
private String reference;
public void setReference(String reference) {
this.reference = reference;
}
public String getReference() {
return reference;
}
}
public class MainActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String reviewsUrl = "https://maps.googleapis.com/maps/api/place/details/json?reference="+reference+sensor+key;
DownloadReviewsTask rev = new DownloadReviewsTask(this, new OnEventListener<HashMap<String, List<MyModel>>>() {
@Override
public void onSuccess(HashMap<String, List<MyModel>> result) {
if(result != null && result.size() > 0) {
for (int i = 0; i < result.size(); i++){
Log.e("review SingleVewInit",result.get("reviews").get(0).getAuthor_name());
Log.e("review SingleVewInit",result.get("reviews").get(0).getText());
}
}
}
@Override
public void onFailure(Exception e) {
//Toast.makeText(this, "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("ERROR: " ,e.getMessage());
}
});
rev.execute(reviewsUrl);
}
}
好的,下面是获取评论的示例代码。您需要了解代码并进行必要的更改才能在您的代码中工作。
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAAkK3AyE8Cbqb9H5MYqptjJwRhRgltoZM");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
int ch = -1;
StringBuffer buffer = new StringBuffer();
while((ch = in.read()) != -1){
buffer.append((char)ch);
}
JSONObject jObj = new JSONObject(buffer.toString());
JSONObject jResult = jObj.getJSONObject("result");
JSONArray jReviewArray = jResult.getJSONArray("reviews");
for(int i=0; i<jReviewArray.length(); i++){
JSONObject jReview = jReviewArray.getJSONObject(i);
System.out.println( jReview.getString("author_name") +"\n"+ jReview.getString("text") +"\n\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
它用 Review_Text 打印 Author_Name。
更新您的代码如下-
- 在DownloadReviewsTask-
改变JSONArray parentArray = json.getJSONArray("reviews");
到JSONArray parentArray = json.getJSONArray("result");
更改setPlace()如下-
public MyModel setPlace(JSONObject thisObject) throws JSONException {
MyModel thisMyModel = new MyModel();
author_name = thisObject.getString("author_name");
text = thisObject.getString("text");
thisMyModel.setAuthor_name(author_name);
thisMyModel.setText(text);
return thisMyModel;
}
我没有在[=中找到setAuthor_name()和setText()方法32=]MyModal 所以改变它如下-
public class MyModel{
private String reference;
private String author_name;
private String text;
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getAuthor_name() {
return author_name;
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getReference() {
return reference;
}
}
最后将 Log stmt 更改为 MainActivity 如下-
Log.e("review SingleVewInit",result.get(i).getAuthor_name());
Log.e("review SingleVewInit",result.get(i).getText());
@kevz
我认为你原来的回答已经足够好了。不需要异步任务。我创建了一个小项目来测试你的想法,它成功了!!!试图将它们与我的代码结合起来会导致一团糟。请看下面,以防对任何人有帮助:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Places">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/text_author"
android:id="@+id/button_id"/>
</RelativeLayout>
及以下,应该足够了:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Places extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new java.net.URL("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAAkK3AyE8Cbqb9H5MYqptjJwRhRgltoZM");
//HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = url.openStream();
int ch = -1;
StringBuffer buffer = new StringBuffer();
while ((ch = in.read()) != -1) {
buffer.append((char) ch);
}
JSONObject jObj = new JSONObject(buffer.toString());
JSONObject jResult = jObj.getJSONObject("result");
JSONArray jReviewArray = jResult.getJSONArray("reviews");
//Float rating = (float)jResult.getDouble("rating");
for (int i = 0; i < jReviewArray.length(); i++) {
JSONObject jReview = jReviewArray.getJSONObject(i);
Log.e("review",jReview.getString("author_name") + "\n" + jReview.getString("text") + "\n\n");
Log.e("rating", jResult.getString("rating") + "\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
这是输出:
Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6
E/review: Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6
E/review: Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6
我检查并确保 android 的 google 个位置 api 已启用。
起初我用的是这个url:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=34.0467359,-118.441764&radius=1000&sensor=true&key=YOUR_PLACE_API_KEY
然后切换到这个:
https://maps.googleapis.com/maps/api/place/details/json? reference=REFERENCE_STRING_OF_THE_PLACE&sensor=true&key=YOUR_PLACE_API_KEY
根据
中的建议Android - Google Places API - No "reviews" Array
但仍然出现同样的错误。
在 https://developers.google.com/places/web-service/details
据说引用已被弃用,现在我们必须使用 place_id。我将 url 更改为:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=YOUR_API_KEY
后来我在 Whosebug 上读到,2012 年 google 地方 API 没有提取评论的功能。这适用于现在吗? 2016年?如果没有,请帮助我在下面的代码中破译我做(正在做)错的地方。
如果我复制并粘贴上述任何 url 的任何日志输出,评论会显示在我的浏览器中,但响应中的输出不会显示 url。
我收到这个错误:
W/System.err: org.json.JSONException: No value for reviews
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
W/System.err: at DownloadReviewsTask.doInBackground(DownloadReviewsTask.java:69)
W/System.err: at DownloadReviewsTask.doInBackground(DownloadReviewsTask.java:19)
W/System.err: at android.os.AsyncTask.call(AsyncTask.java:292)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
下面是我的代码:
public class JSONParser {
String charset = "UTF-8";
HttpURLConnection conn;
DataOutputStream wr;
StringBuilder result = new StringBuilder();
URL urlObj;
JSONObject jObj = null;
StringBuilder sbParams;
String paramsString;
public JSONObject makeHttpRequest(String url, String method,
HashMap<String, String> params) {
sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
if (method.equals("POST")) {
// request method is POST
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", charset);
conn.setReadTimeout(90000);
conn.setConnectTimeout(90000);
conn.connect();
paramsString = sbParams.toString();
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(90000);
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
// try parse the string to a JSON object
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
}
public class DownloadReviewsTask extends AsyncTask<String, Void, HashMap<String, List<MyModel>>> {
//Declarations
private Context mContext;
public Exception mException;
private OnEventListener<HashMap<String, List<MyModel>>> mCallBack;
JSONArray reviews;
String author_name;
String text;
//Constructor
public DownloadReviewsTask(Context context, OnEventListener callback) {
mCallBack = callback;
mContext = context;
}
//sets and passes the review author and text for each review and sets them in the My Model
public MyModel setPlace(JSONObject thisObject) throws JSONException {
JSONObject results = thisObject.getJSONObject("results");
MyModel thisMyModel = new MyModel();
if (!results.isNull("reviews")) {
reviews = results.getJSONArray("reviews");
Log.e("reviews array setPlace", String.valueOf(reviews));
for (int i = 0; i < reviews.length(); i++) {
//if(i==reviews.length()) break;
// if (thisObject.getJSONArray("reviews").getJSONObject(i).has("author_name")) //{
author_name = results.getJSONArray("reviews").getJSONObject(i).getString("author_name");//reviews.getJSONObject(i).getString("author");
Log.e("review_author in setup", results.getJSONArray("reviews").getJSONObject(i).getString("author_name"));
// }
//if (thisObject.getJSONArray("reviews").getJSONObject(i).has("text")) {
Log.e("review text in setup", results.getJSONArray("reviews").getJSONObject(i).getString("text"));
text = results.getJSONArray("reviews").getJSONObject(i).getString("text");//}
thisMyModel.setAuthor_name(author_name);
thisMyModel.setText(text);
}
}
return thisMyModel;
}
@Override
protected HashMap<String, List<MyModel>> doInBackground(String... args) {
try {
JSONParser jsonParser = new JSONParser();
JSONObject json;
String reviewUrl = args[0];
Log.e("reviewUrl",reviewUrl);
//Use a HashMap instead with the varargs:
HashMap<String, String> params = new HashMap<>();
json = jsonParser.makeHttpRequest(reviewUrl, "GET", params);
HashMap<String, List<MyModel>> myResults = new HashMap<>();
if(json != null) {
Log.e("reviews result", json.toString());
JSONArray parentArray = json.getJSONArray("reviews");
List<MyModel> allEventsList = new ArrayList<>();
//loops through json array
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
allEventsList.add(setPlace(finalObject));
Log.e("allEventsList", String.valueOf(allEventsList));
}
myResults.put("reviews", allEventsList);
Log.e("myResults",String.valueOf(myResults));
return myResults;
}else{
return null;
}
}
catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, List<MyModel>> result) {
if (mCallBack != null) {
if (mException == null) {
mCallBack.onSuccess(result);
} else {
mCallBack.onFailure(mException);
}
}
}
}
public class MyModel{
private String reference;
public void setReference(String reference) {
this.reference = reference;
}
public String getReference() {
return reference;
}
}
public class MainActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String reviewsUrl = "https://maps.googleapis.com/maps/api/place/details/json?reference="+reference+sensor+key;
DownloadReviewsTask rev = new DownloadReviewsTask(this, new OnEventListener<HashMap<String, List<MyModel>>>() {
@Override
public void onSuccess(HashMap<String, List<MyModel>> result) {
if(result != null && result.size() > 0) {
for (int i = 0; i < result.size(); i++){
Log.e("review SingleVewInit",result.get("reviews").get(0).getAuthor_name());
Log.e("review SingleVewInit",result.get("reviews").get(0).getText());
}
}
}
@Override
public void onFailure(Exception e) {
//Toast.makeText(this, "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("ERROR: " ,e.getMessage());
}
});
rev.execute(reviewsUrl);
}
}
好的,下面是获取评论的示例代码。您需要了解代码并进行必要的更改才能在您的代码中工作。
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAAkK3AyE8Cbqb9H5MYqptjJwRhRgltoZM");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
int ch = -1;
StringBuffer buffer = new StringBuffer();
while((ch = in.read()) != -1){
buffer.append((char)ch);
}
JSONObject jObj = new JSONObject(buffer.toString());
JSONObject jResult = jObj.getJSONObject("result");
JSONArray jReviewArray = jResult.getJSONArray("reviews");
for(int i=0; i<jReviewArray.length(); i++){
JSONObject jReview = jReviewArray.getJSONObject(i);
System.out.println( jReview.getString("author_name") +"\n"+ jReview.getString("text") +"\n\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
它用 Review_Text 打印 Author_Name。
更新您的代码如下-
- 在DownloadReviewsTask-
改变JSONArray parentArray = json.getJSONArray("reviews");
到JSONArray parentArray = json.getJSONArray("result");
更改setPlace()如下-
public MyModel setPlace(JSONObject thisObject) throws JSONException { MyModel thisMyModel = new MyModel(); author_name = thisObject.getString("author_name"); text = thisObject.getString("text"); thisMyModel.setAuthor_name(author_name); thisMyModel.setText(text); return thisMyModel; }
我没有在[=中找到setAuthor_name()和setText()方法32=]MyModal 所以改变它如下-
public class MyModel{ private String reference; private String author_name; private String text; public void setAuthor_name(String author_name) { this.author_name = author_name; } public String getAuthor_name() { return author_name; } public void setText(String text) { this.text = text; } public String getText() { return text; } public void setReference(String reference) { this.reference = reference; } public String getReference() { return reference; } }
最后将 Log stmt 更改为 MainActivity 如下-
Log.e("review SingleVewInit",result.get(i).getAuthor_name()); Log.e("review SingleVewInit",result.get(i).getText());
@kevz
我认为你原来的回答已经足够好了。不需要异步任务。我创建了一个小项目来测试你的想法,它成功了!!!试图将它们与我的代码结合起来会导致一团糟。请看下面,以防对任何人有帮助:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Places">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/text_author"
android:id="@+id/button_id"/>
</RelativeLayout>
及以下,应该足够了:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Places extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new java.net.URL("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAAkK3AyE8Cbqb9H5MYqptjJwRhRgltoZM");
//HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = url.openStream();
int ch = -1;
StringBuffer buffer = new StringBuffer();
while ((ch = in.read()) != -1) {
buffer.append((char) ch);
}
JSONObject jObj = new JSONObject(buffer.toString());
JSONObject jResult = jObj.getJSONObject("result");
JSONArray jReviewArray = jResult.getJSONArray("reviews");
//Float rating = (float)jResult.getDouble("rating");
for (int i = 0; i < jReviewArray.length(); i++) {
JSONObject jReview = jReviewArray.getJSONObject(i);
Log.e("review",jReview.getString("author_name") + "\n" + jReview.getString("text") + "\n\n");
Log.e("rating", jResult.getString("rating") + "\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
这是输出:
Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6
E/review: Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6
E/review: Justine OBRIEN
Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on!
E/rating: 4.6
E/review: James Pallett
I can not understand why Google would choose Titan Digital as one their 14 Premier Partners. It was on this basis that I choose Titan to build my new web site. It is costing me dearly. All words, no action, over promise, under deliver. My second biggest complaint, of many, about Titan, after the total mismanagement of my project, is Titan's lack of communication. Days and weeks go by without a word. Inquiries are either ignored or treated with indifference. Almost eight months have passed since I signed up for a new web site and inquiries receive no urgency, only platitudes. To add insult to injury, Titan has the audacity to charge for monthly web maintenance for a web site still long overdue in production. Be warned.
E/rating: 4.6
E/review: Dorothy Ooko
Cool offices.... Reception on the 5th floor. Best coffee barrista is on the 4thð
E/rating: 4.6
E/review: Taylor Hayes
Absolute joke trying to find who to deal with in regards to services i have paid for. Happy to take the money and told its all easy . . . .weeks i have waited to finish verifying my coffee shop after purchasing a virtual tour pathetic aftercare
E/rating: 4.6
E/review: TheHealthRetreat
I spend 250 thousand dollars a year with Google adwords..
I own a Mental Health Drug and alcohol retreat.
E/rating: 4.6