Android 试一试什么也没发生
在这个活动中,我在附近找到位置并将它们添加到列表视图中。我还想像其他数据一样在arrayList中添加地点的电话号码,所以我必须使用地点详细信息请求。因此,我从arrayList中获取所有位置的所有place_id,并启动查询以获取详细信息(电话号码)。问题出在课堂上“从Google PlaceDetailsAPI读取”,它出现在“尝试””中,并且没有发生任何事情,我不知道为什么!!!我只能从println中看到“IN TRY!!!”和“----” 我的顺序不对吗? 问题在哪里?解决方案是什么?Android 试一试什么也没发生,android,json,arraylist,google-places-api,Android,Json,Arraylist,Google Places Api,在这个活动中,我在附近找到位置并将它们添加到列表视图中。我还想像其他数据一样在arrayList中添加地点的电话号码,所以我必须使用地点详细信息请求。因此,我从arrayList中获取所有位置的所有place_id,并启动查询以获取详细信息(电话号码)。问题出在课堂上“从Google PlaceDetailsAPI读取”,它出现在“尝试””中,并且没有发生任何事情,我不知道为什么!!!我只能从println中看到“IN TRY!!!”和“----” 我的顺序不对吗? 问题在哪里?解决方案是什么?
public class ListActivity extends Activity implements OnItemClickListener {
public ArrayList<GetterSetter> myArrayList;
ArrayList<GetterSetter> detailsArrayList;
ListView myList;
ProgressDialog dialog;
TextView nodata;
CustomAdapter adapter;
GetterSetter addValues;
GetterSetter addDetails;
private LocationManager locMan;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_activity);
if (!isNetworkAvailable()) {
Toast.makeText(getApplicationContext(), "Enable internet connection and RE-LAUNCH!!",
Toast.LENGTH_LONG).show();
return;
}
myList = (ListView) findViewById(R.id.placesList);
placeSearch();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
public void placeSearch() {
//get location manager
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//get last location
Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lat = lastLoc.getLatitude();
double lng = lastLoc.getLongitude();
dialog = ProgressDialog.show(this, "", "Please wait", true);
//build places query string
String placesSearchStr;
placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location="+lat+","+lng+
"&radius=1000&sensor=true" +
"&types="+ ServicesListActivity.types+
"&key=My_KEY";
//execute query
new readFromGooglePlaceAPI().execute(placesSearchStr);
myList.setOnItemClickListener(this);
}
public void detailsSearch() {
String detailsSearchStr;
//build places query string
for(int i=0; i < myArrayList.size(); i++){
detailsSearchStr = "https://maps.googleapis.com/maps/api/place/details/json?" +
"placeid=" + myArrayList.get(i).getPlace_id() +
"&key=My_KEY";
Log.d("PlaceID:", myArrayList.get(i).getPlace_id());
//execute query
new readFromGooglePlaceDetailsAPI().execute(detailsSearchStr);
}
}
public class readFromGooglePlaceDetailsAPI extends AsyncTask<String, Void, String> {
@Override protected String doInBackground(String... param) {
return readJSON(param[0]);
}
protected void onPostExecute(String str) {
detailsArrayList = new ArrayList<GetterSetter>();
String phoneNumber =" -NA-";
try {
System.out.println("IN TRY !!!");
JSONObject root = new JSONObject(str);
JSONArray results = root.getJSONArray("result");
System.out.println("Before FOR !!!");
for (int i = 0; i < results.length(); i++) {
System.out.println("IN FOR LOOP !!!");
addDetails = new GetterSetter();
JSONObject arrayItems = results.getJSONObject(i);
if(!arrayItems.isNull("formatted_phone_number")){
phoneNumber = arrayItems.getString("formatted_phone_number");
Log.d("Phone Number ", phoneNumber);
}
addDetails.setPhoneNumber(phoneNumber);
System.out.println("ADDED !!!");
detailsArrayList.add(addDetails);
Log.d("Before", detailsArrayList.toString());
}
} catch (Exception e) {
}
System.out
.println("------------------------------------------------------------------");
Log.d("After:", detailsArrayList.toString());
// nodata = (TextView) findViewById(R.id.nodata);
//nodata.setVisibility(View.GONE);
// adapter = new CustomAdapter(ListActivity.this, R.layout.list_row, detailsArrayList);
// myList.setAdapter(adapter);
//adapter.notifyDataSetChanged();
// dialog.dismiss();
}
}
public class readFromGooglePlaceAPI extends AsyncTask<String, Void, String> {
@Override protected String doInBackground(String... param) {
return readJSON(param[0]);
}
protected void onPostExecute(String str) {
myArrayList = new ArrayList<GetterSetter>();
String rating=" -NA-";
try {
JSONObject root = new JSONObject(str);
JSONArray results = root.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
addValues = new GetterSetter();
JSONObject arrayItems = results.getJSONObject(i);
JSONObject geometry = arrayItems.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
//place ID for place details later
String placeID = arrayItems.getString("place_id").toString();
if(!arrayItems.isNull("rating")){
rating = arrayItems.getString("rating");
}
addValues.setPlace_id(placeID);
addValues.setLat(location.getString("lat"));
addValues.setLon(location.getString("lng"));
addValues.setName(arrayItems.getString("name").toString());
addValues.setRating(rating);
addValues.setVicinity(arrayItems.getString("vicinity").toString());
myArrayList.add(addValues);
//Log.d("Before", myArrayList.toString());
}
} catch (Exception e) {
}
// System.out
// .println("############################################################################");
// Log.d("After:", myArrayList.toString());
nodata = (TextView) findViewById(R.id.nodata);
nodata.setVisibility(View.GONE);
adapter = new CustomAdapter(ListActivity.this, R.layout.list_row, myArrayList);
myList.setAdapter(adapter);
//adapter.notifyDataSetChanged();
dialog.dismiss();
detailsSearch();
}
}
public String readJSON(String URL) {
StringBuilder sb = new StringBuilder();
HttpGet httpGet = new HttpGet(URL);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} else {
Log.e("JSON", "Couldn't find JSON file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent details = new Intent(ListActivity.this, Details.class);
details.putExtra("name", myArrayList.get(arg2).getName());
details.putExtra("rating", myArrayList.get(arg2).getRating());
details.putExtra("vicinity", myArrayList.get(arg2).getVicinity());
details.putExtra("lat", myArrayList.get(arg2).getLat());
details.putExtra("lon", myArrayList.get(arg2).getLon());
details.putExtra("formatted_phone_number", detailsArrayList.get(arg2).getPhoneNumber());
startActivity(details);
}
}
公共类ListActivity扩展活动实现了ListListener{
公共ArrayList myArrayList;
ArrayList detailsArrayList;
列表视图myList;
进程对话;
文本视图节点数据;
自定义适配器;
GetterSetter附加值;
GetterSetter添加详细信息;
私人场所经理洛克曼;
@在创建时覆盖受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list\u view\u活动);
如果(!isNetworkAvailable()){
Toast.makeText(getApplicationContext(),“启用internet连接并重新启动!!”,
Toast.LENGTH_LONG).show();
返回;
}
myList=(ListView)findViewById(R.id.placesList);
placeSearch();
}
专用布尔值isNetworkAvailable(){
ConnectivityManager ConnectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo activeNetworkInfo=connectivityManager.getActiveNetworkInfo();
返回activeNetworkInfo!=null;
}
公共搜索(){
//获取位置管理器
locMan=(LocationManager)getSystemService(Context.LOCATION\u服务);
//获取最后位置
Location lastLoc=locMan.getlastnownlocation(LocationManager.NETWORK\u PROVIDER);
双纬度=lastLoc.getLatitude();
double lng=lastLoc.getLongitude();
dialog=ProgressDialog.show(此“,”请稍候“,true);
//生成位置查询字符串
字符串placesSearchStr;
placesSearchStr=”https://maps.googleapis.com/maps/api/place/nearbysearch/" +
“json”位置=“+lat+”,“+lng+
“&radius=1000&sensor=true”+
“&types=“+ServicesListActivity.types”+
“&key=My_key”;
//执行查询
新的readFromGooglePlaceAPI().execute(placesSearchStr);
myList.setonicmclicklistener(this);
}
公共无效详细信息搜索(){
字符串detailsSearchStr;
//生成位置查询字符串
对于(int i=0;icatch (Exception e) {
}
catch (Exception e) {
Log.e(YOUR_TAG, "Exception ..." , e);
}
try{
JSONObject jsonObject = new JSONObject(str);
if (jsonObject.has("results")) {
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
//your logic here
}
}
} catch (JSONException e) {
e.printStackTrace();
}