如何在Android的使用方法AsyncTask的? [英] how to use method in AsyncTask in android?

查看:94
本文介绍了如何在Android的使用方法AsyncTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序使用 JSON Web服务来从数据谷歌导航API 。我用的是code是如下。 我得到了以下异常

In my application I use JSON webservice to get data from Google Navigation api. I use the code is below. I got the following exception

android.os.NetworkOnMainThreadException

如何使用AsyncTask的?这里是我的code。 Thanks.`public类MainActivity扩展MapActivity {

How to use AsyncTask? here is my code. Thanks.`public class MainActivity extends MapActivity {

  MapView mapView ;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    System.out.println("*************1**************1");
    setContentView(R.layout.activity_main);
    System.out.println("*************2**************");
    mapView = (MapView) findViewById(R.id.mapv); 
    System.out.println("*************3**************");



                Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));

              RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
              mapView.getOverlays().add(routeOverlay);
            mapView.invalidate();


    System.out.println("*************4**************");

}



@SuppressLint("ParserError")
private Route directions(final GeoPoint start, final GeoPoint dest) {



    //https://developers.google.com/maps/documentation/directions/#JSON <- get api
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");

    Parser parser = new GoogleParser(sBuf.toString());
      Route   r =  parser.parse();
   System.out.println("********r in thread*****" +r);                  
            return r;
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

} `

推荐答案

@imran汗的想法是正确的。从蜂巢及以上的,你不能做任何网络操作的UI线程内。的UI线程负责显示视图和接收的触摸事件,并且它必须是自由此,没有被长时间运行的操作的线程。 (否则你有在Android的ANR)。

@imran khan idea of using another thread is the right one. From HoneyComb and above, you can't do any networking operation inside the UI Thread. The UI Thread is the thread responsible for displaying views and receiving touch events and it must be free for this, not blocked by a long running operation. (Otherwise you got an ANR on Android).

但它实际上不是使用AsyncTask的像一个网络请求长时间运行的操作一个非常好的主意。该AsyncTask的的javadoc甚至各州,他们已经设计了短捉迷藏操作...

But it's actually not a very good idea to use an AsyncTask for long running operations like a network Request. The AsyncTask javadocs even states they have been designed for short runnning operations...

最好的方法是使用一个服务的。我建议您从阅读本信息图表 RoboSpice库,并使用 RoboSpice ,这将减轻创建与网络服务,并使其更容易为你的使用春天的Andr​​oid 解析您的JSON内容。

The best approach is to use a service for that. I encourage you to read this infographics from the RoboSpice library and use RoboSpice, it will ease creating a service with networking and will make it easier for your to parse your json content using Spring Android.

这篇关于如何在Android的使用方法AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆