Can';不能使Asynctask在不同的android版本中工作

Can';不能使Asynctask在不同的android版本中工作,android,android-asynctask,versions,Android,Android Asynctask,Versions,我正在使用以下代码: public void run() { handler.post(new Runnable() { public void run() { try { if (count==true) { try

我正在使用以下代码:

  public void run() {
        handler.post(new Runnable() {
            public void run() {       
                try {
                    if (count==true)
                    {        
                        try 
                        {
                            r.stop();
                        } catch (Exception e) {}
                        tranca=false;
                        count=false; 

                        dlg.dismiss();
                        dlg.cancel();
                        dlg.dismiss();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
                             //task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
                        } else {
                            new AsyncCaller().execute();
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                }
            }
        });
    }
}, 15000);
问题是我不知道怎么做 新建AsyncCaller().executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR)

工作。问题是asynctask在honeycomb之后改变了它的工作方式,我发现了这段代码。它适用于蜂窝下面的Android版本,但在我的jellybean中它不起作用。我可能正在使用新的AsyncCaller().executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR) 以错误的方式。 任务如下:

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


        JSONObject ret = getLocationInfo(); 
        JSONObject location2;
        String location_string;
        try {
            location2 = ret.getJSONArray("results").getJSONObject(0);
            location_string = location2.getString("formatted_address");
            Log.d("test", "formattted address:" + location_string);
            StringRua = (" " + location_string);
        } catch (JSONException e1) {
            e1.printStackTrace();

        }

         StringSMS = (" Latitude " + StringLat + " Longitude " + StringLon + " Ad " + StringRua);   




        EditText Search01; 
        String String01;

        Search01 = (EditText) findViewById(R.id.Search01);
        String01 = Search01.getText().toString();  


        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(String01, null, StringSMS , null, null);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
    }

    }   
私有类AsyncCaller扩展AsyncTask
{
公共JSONObject getLocationInfo(){
HttpGet HttpGet=新的HttpGet(“http://maps.google.com/maps/api/geocode/json?latlng=“+latitude1+”、“+longitude2+”&sensor=true”);
HttpClient=new DefaultHttpClient();
HttpResponse响应;
StringBuilder StringBuilder=新的StringBuilder();
试一试{
response=client.execute(httpGet);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
int b;
而((b=stream.read())!=-1){
stringBuilder.append((char)b);
}
}捕获(客户端协议例外e){
}捕获(IOE异常){
}
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject=新的jsonObject(stringBuilder.toString());
}捕获(JSONException e){
e、 printStackTrace();
}
返回jsonObject;
}
ProgressDialog pdLoading=新建ProgressDialog(MainActivity.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//此方法将在UI线程上运行
pdLoading.setMessage(“\t加载…”);
pdLoading.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//此方法将在后台线程上运行,因此不要在此处更新UI
//在这里执行长时间运行的http任务,您不想传递参数,您可以在这里访问父类的变量url
JSONObject ret=getLocationInfo();
JSONObject位置2;
字符串位置\u字符串;
试一试{
location2=ret.getJSONArray(“结果”).getJSONObject(0);
location_string=location2.getString(“格式化的_地址”);
Log.d(“测试”,“格式化地址:”+位置\字符串);
StringRua=(“”+位置_字符串);
}捕获(JSONException e1){
e1.printStackTrace();
}
StringSMS=(“纬度”+StringLat+“经度”+StringLon+“Ad”+StringRua);
编辑文本搜索01;
字符串String01;
Search01=(EditText)findViewById(R.id.Search01);
String01=Search01.getText().toString();
SmsManager SmsManager=smsmsmanager.getDefault();
sendTextMessage(String01,null,StringSMS,null,null);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//此方法将在UI线程上运行
pdLoading.disclose();
}
}   
我知道stackoverflow中也有同样的问题,但我尝试了很多解决方案,似乎并没有人适合我。我只需要为JellyBean设备适当地调用asynctask。请记住,新建AsyncCaller().execute()在蜂巢下方的设备中完美工作。 多谢各位


编辑:仍然不工作,它不执行Jelly Bean设备中dointhebackground中的内容(它显示加载,所以我知道它发送到AsynctTask)。我已经试着把需要做的事情放到前台和后台之外,但我不能这样做,因为“NetworkOnMainThreadException”问题,这只会迫使在新的android版本上关闭程序

如果希望在不同的线程池执行器上执行异步任务,请在此处复制异步任务代码,并通过调用execute将其用于前HC和后HC。对于HC和更高版本中的异步任务,使用的内部方法是串行执行器,除非您指定自己的执行器并将其传递给execute方法

如果您不想使用该代码,您仍然可以为前HC和后HC调用execute,如果(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.HONEYCOMB),它应该可以正常工作, 删除此处的
=
。如果您的BuildVersion是API11,那么它已经可以连续运行了


尝试将您的targetSdk更改为最新版本。异步任务的处理方式(串行/并行)取决于targetSdk。阅读有关AsyncTask陷阱的更多信息

你的minSdk和targetSdk是什么?安卓:minSdkVersion=“8”安卓:targetSdkVersion=“11”检查下面的我的答案,不要尝试使用troll,而是定义“不工作”。您正在从非UI线程的线程上的屏幕组件读取数据。所以我猜您正在读取的数字是空的,doInBackground是no op。为什么不让AsyncCaller成为AsyncCaller并实际定义广播的PendingEvent呢。这样,在UI线程上就可以有executeOnExecutor(executor,String.valueOf(textView.getText());并且成功的结果将被广播。另外,请看一下它显然对我的ICS设备起到了作用(这也不起作用)。jelly bean手机是我朋友的手机,他现在不在这里,我知道,但稍后他会测试它,我会把结果放在这里,如果成功,我会接受这个作为答案。谢谢你。不工作,他测试了它,它会加载,但它不会执行dointhebackground中的内容。我已经尝试将dointhebackground放在dointhebackground之外Kglound需要做什么,但我不能这样做,因为“NetworkOnMainThreadException”问题,这只会迫使在新的android版本上关闭程序。是的,