我正在尝试运行 AsyncTask 以从 REST API 返回结果,但我编写的代码似乎没有运行。当我通过应用程序运行到应该运行 AsyncTask 的位置时,什么都没有显示。没有错误,数据永远不会显示。此外,当我输入 created some log.i
s 来显示返回的数据时,LogCat 窗口中没有显示任何内容。
以下是相关代码:
public class PlaceActivity extends FragmentActivity {
public static final String EXTRA_URL = "url";
public static final String GEO = "geo";
public static final LatLng LOCATION = new LatLng(53.551, 9.993);
private GoogleMap mMap;
protected Factual factual = new Factual("xyz", "xyz"); //new Factual code
private TextView resultText = null; //new Factual code
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place);
setupActionBar();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(EXTRA_URL);
String geo = extras.getString(GEO);
Log.i("geo", geo); //currently from_user
Log.i("name", name); //currently text
TextView venueLocation = (TextView) findViewById(R.id.vLocation); //was detailsText
//view.setText(name + " " + geo);
TextView venName = (TextView) findViewById(R.id.vName);
venueLocation.setText(geo);
venName.setText(name);
setUpMapIfNeeded();
factualQuery(); //new Factual code
}
}
private void setUpMapIfNeeded() {
//Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
//do things to the map
mMap.addMarker(new MarkerOptions().position(LOCATION).title(EXTRA_URL));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION,15));
mMap.getUiSettings().setZoomControlsEnabled(false);
}
}
}
public void factualQuery() {
resultText = (TextView) findViewById(R.id.resultText);
FactualRetrievalTask task = new FactualRetrievalTask();
double latitude = 34.06018; //set programmatically
double longitude = -118.41835; //set programmatically
int meters = 500; //have a default and then set programmatically if needed?
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Query query = new Query()
.within(new Circle(latitude, longitude, meters))
.field("cuisine").equal("Italian")
.sortAsc("$distance")
.only("name", "address", "tel");
task.execute(query);
}
public class FactualRetrievalTask extends AsyncTask<Query, Integer, List<ReadResponse>> {
@Override
protected List<ReadResponse> doInBackground(Query... params) {
List<ReadResponse> results = Lists.newArrayList();
for (Query q : params) {
results.add(factual.fetch("restaurants-us", q));
}
return results;
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(List<ReadResponse> responses) {
StringBuffer sb = new StringBuffer();
for (ReadResponse response : responses) {
for (Map<String, Object> restaurant : response.getData()) {
String name = (String) restaurant.get("name");
Log.i("name", name);
String address = (String) restaurant.get("address");
Log.i("address", address);
String phone = (String) restaurant.get("tel");
Log.i("tel", phone);
Number distance = (Number) restaurant.get("$distance");
sb.append(distance + " meters away: "+name+" @ " +address + ", call "+phone);
sb.append(System.getProperty("line.separator"));
}
}
resultText.setText(sb.toString());
}
}
}
log.i
inonPostExecute()
似乎没有显示任何内容,这让我相信它没有被执行。奇怪的是,这段代码以前在不同的项目中运行,没有任何问题。
我当时认为这可能是与依赖关系有关的问题(因为这是围绕此代码的问题),但正如我所说,我没有收到任何错误。依赖关系仍然是一个问题吗?有什么看起来很奇怪吗?
谢谢!