2013-04-22 107 views
2

我想运行一个AsyncTask来从REST API返回结果,但我写的代码似乎没有运行。当我运行应用程序到应该运行AsyncTask的时候,什么都不显示。没有错误,数据永远不会显示。此外,当我输入创建的某个log.i以显示正在返回的数据时,LogCat窗口中未显示任何内容。我的AsyncTask有什么问题?似乎没有正确执行

以下是相关的代码:

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()); 
    } 
} 

}

onPostExecute()log.i不似乎呈现出任何东西,这使我相信,它没有被执行。奇怪的是,这个代码以前在不同的项目中运行没有任何问题。

我在想这可能是一个与依赖关系有关的问题(因为这是围绕此代码的问题),但正如我所说,我没有得到任何错误。依赖关系仍然是一个问题?有什么奇怪的东西吗?

谢谢!

+0

似乎看不出什么奇怪的东西 - 你确定查询返回什么吗? – ddmps 2013-04-22 01:41:39

+0

不,我不是。确定它们是否是最好的方法是什么? – user2163853 2013-04-22 01:43:23

+0

尝试不使用异步来测试代码是否真正起作用。 – 2013-04-22 06:28:27

回答

0

您是在模拟器中测试吗?如果是,模拟器默认情况下不会给你位置,你必须提供它的坐标,然后它会提供给你的应用程序。请参阅http://developer.android.com/tools/devices/emulator.html#geo 您可以尝试在设备上运行它,并查看它是否运行您的网络提取。

+0

不,我正在用Android 2.3.4测试HTC Wildfire,通过wifi连接。我开始这样做是因为代码的地图部分。 – user2163853 2013-04-22 01:45:49

0

首先,在doInBackground()中添加一些Log.d来检查aynctask是否被网络阻塞。 还有其他的aynctask在运行吗? AsyncTask默认只使用单线程,所以它会被其他Asynctask阻塞。

0

原来这是一个依赖关系的问题。我尝试了以前没有工作的旧项目,并突然开始工作。我将当前的项目与其他项目的依赖关系进行了匹配,并解决了问题。我最初犹豫要这样做,因为我在之前的项目及其依赖关系中遇到了构建错误,但是我删除了导致该问题的原因,现在我认为我很好。然而,为什么依赖关系可以解决这个问题,但我不会得到错误告诉我某些方法/变量丢失了(即NullPointer,NoSuchMethod等)吗?