java–AsyncHttpClient Error中使用的同步ResponseHandler

java–AsyncHttpClient Error中使用的同步ResponseHandler,第1张

概述最后一天,我正在尝试使用我的asynchttpclient修复错误,但我创建的帮助并没有解决问题.希望有人可以帮助我.错误是当我启动服务时,我希望该任务总是使用php进行sql查询并且它接收我稍后将使用的参数.Erroris:01-3000:20:25.3194167-4671/proyecto.androidW/AsyncHttpRH:Curr

最后一天,我正在尝试使用我的asynchttpclIEnt修复错误,但我创建的帮助并没有解决问题.
希望有人可以帮助我.
错误是当我启动服务时,我希望该任务总是使用PHP进行SQL查询并且它接收我稍后将使用的参数.

Error is:01-30 00:20:25.319 4167-4671/proyecto.androID W/AsynchttpRH: Current thread has not called Looper.prepare(). Forcing synchronous mode.01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err: java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsynchttpClIEnt. You should create your response handler in a looper thread or use SynchttpClIEnt instead.01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at com.loopj.androID.http.AsynchttpClIEnt.sendRequest(AsynchttpClIEnt.java:1493)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at com.loopj.androID.http.AsynchttpClIEnt.post(AsynchttpClIEnt.java:1169)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at com.loopj.androID.http.AsynchttpClIEnt.post(AsynchttpClIEnt.java:1152)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at com.loopj.androID.http.AsynchttpClIEnt.post(AsynchttpClIEnt.java:1139)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at proyecto.androID.MyService.ConsultaCaract(MyService.java:128)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at proyecto.androID.MyService$MyTask.doInBackground(MyService.java:72)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at proyecto.androID.MyService$MyTask.doInBackground(MyService.java:61)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at androID.os.AsyncTask.call(AsyncTask.java:288)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)01-30 00:20:25.320 4167-4671/proyecto.androID W/System.err:     at androID.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)01-30 00:20:25.321 4167-4671/proyecto.androID W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)01-30 00:20:25.321 4167-4671/proyecto.androID W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)01-30 00:20:25.321 4167-4671/proyecto.androID W/System.err:     at java.lang.Thread.run(Thread.java:818)

我的代码是这样的:

package proyecto.androID;import androID.app.Service;import androID.bluetooth.BluetoothAdapter;import androID.content.Context;import androID.content.Intent;import androID.media.MediaPlayer;import androID.net.wifi.WifiManager;import androID.os.AsyncTask;import androID.os.Handler;import androID.os.IBinder;import androID.os.Vibrator;import androID.util.Log;import androID.Widget.Toast;import com.loopj.androID.http.AsynchttpClIEnt;import com.loopj.androID.http.AsynchttpResponseHandler;import com.loopj.androID.http.RequestParams;import org.Json.JsONArray;import java.util.ArrayList;public class MyService extends Service {    MediaPlayer  mp;    String email="123@123.com";    MyTask task= new MyTask();  //  Bundle miBundle= getIntent().getExtras();  // Bundle datosext = getIntent().getExtras();   // String email=datosext.getString("email");    @OverrIDe    public voID onCreate() {        super.onCreate();    }    //Al pulsar el boton de iniciar servicio se ejecutara onStartCommand    @OverrIDe    public int onStartCommand(Intent intent, int flags, int startID) {        Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_SHORT).show();        try {            task.execute();        }catch (Exception e){            e.printstacktrace();        }        return START_STICKY;    }    //Al pulsar el boton de destruir servicio se ejecutara ondestroy    @OverrIDe    public voID onDestroy(){        Toast.makeText(this, "Servicio DetenIDo", Toast.LENGTH_SHORT).show();        task.onCancelled();    }    private class MyTask extends AsyncTask<String, String, String>{        boolean time=true;        int contador=0;        @OverrIDe        protected String doInBackground(String... params) {        while(time) {            try {                Log.e("Entro", "En log dentro de while: " + email + " " + (++contador));                Thread.sleep(2000);                //ConsultaCaract(email);                ConsultaCaract(email);            } catch (Exception e) {                e.printstacktrace();            }        }            return null;        }        @OverrIDe        protected voID onPostExecute(String s) {           super.onPostExecute(s);            new Handler().postDelayed(new Runnable() {                public voID run() {                    ConsultaCaract(email);                }            }, 10000);        }        @OverrIDe        protected voID onCancelled() {            super.onCancelled();            time=false;        }        @OverrIDe        protected voID onProgressUpdate(String... values) {            super.onProgressUpdate(values);            //inicio();        }    }    public voID inicio(){        ConsultaCaract(email);    }    @OverrIDe    public IBinder onBind(Intent intent) {        return null;    }    public voID ConsultaCaract(String email) {        String url="http://localhost/scripts/Envio_Datos/androID_functions.PHP";        RequestParams params = new RequestParams();        params.put("email", email);        AsynchttpClIEnt clIEnte = new AsynchttpClIEnt();        clIEnte.post(url, params, new AsynchttpResponseHandler() {            @OverrIDe            public voID onSuccess(int i, cz.msebera.androID.httpclIEnt.header[] headers, byte[] bytes) {                getDataJson(new String(bytes));            }            @OverrIDe            public voID onFailure(int i, cz.msebera.androID.httpclIEnt.header[] headers, byte[] bytes, Throwable throwable) {                conection_error();            }        });    }    //Toast indicando error para la conexión ocn la BD    public voID conection_error(){        Vibrator vibrator =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);        vibrator.vibrate(200);        Toast errtoast = Toast.makeText(getApplicationContext(),"ServIDor: Hay un problema con el servIDor, intentelo mas tarde.", Toast.LENGTH_SHORT);        errtoast.show();    }    //Se crea un Array en JsON por el cual obtenemos los valores logstatus(0 o 1 segun si existe o no) y el campo Nombre de la consulta sql    public ArrayList<String > getDataJson(String response){        ArrayList<String> Listado=new ArrayList<String>();        try{            JsONArray JsonArray= new JsONArray(response);            String Wifi,Bluetooth, email,Camera, GPS, Vibracion, Alarma;            for(int i =0; i<JsonArray.length();i++){                Wifi=JsonArray.getJsONObject(i).getString("Wifi");                Bluetooth=JsonArray.getJsONObject(i).getString("Bluetooth");                Camera=JsonArray.getJsONObject(i).getString("Camera");                GPS=JsonArray.getJsONObject(i).getString("GPS");                Vibracion=JsonArray.getJsONObject(i).getString("Vibracion");                Alarma=JsonArray.getJsONObject(i).getString("Alarma");                ComprFunc(Wifi, Bluetooth, Camera, GPS, Vibracion, Alarma);            }        }        catch (Exception e){            e.printstacktrace();        }        return Listado;    }    //Funcion para iniciar aplicaciones de movil.    public voID ComprFunc(String Wifi, String Bluetooth, String Camera, String GPS, String Vibracion, String Alarma){        WifiManager admin_wifi=(WifiManager)this.getSystemService(Context.WIFI_SERVICE);        mp = MediaPlayer.create(this, R.raw.sirena);        BluetoothAdapter adapter_bluetooth=BluetoothAdapter.getDefaultAdapter();        if(Wifi.equals("1")){            admin_wifi.setWifIEnabled(admin_wifi.isWifIEnabled());            Wifi="0";        }        else{            admin_wifi.setWifIEnabled(!admin_wifi.isWifIEnabled());        }        if (Alarma.equals("1")) {            mp.start();        } else{            mp.stop();        }        if(Vibracion.equals("1")){            Vibrator vibrator =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);            vibrator.vibrate(3500);        }        if(Bluetooth.equals("1")){            adapter_bluetooth.enable();        }        else{            adapter_bluetooth.disable();        }    }}

感谢未来的帮助!!

解决方法:

错误“AsynchttpClIEnt中使用的同步ResponseHandler.您应该在循环线程中创建响应处理程序或使用SynchttpClIEnt.”提示问题是什么.

因为在这种情况下你想使用AsynchttpClIEnt而不是SynchttpClIEnt,你需要使用looper线程.

Handler mainHandler = new Handler(Looper.getMainLooper());Runnable myRunnable = new Runnable() {           @OverrIDe            public voID run() {                //Code that uses AsynchttpClIEnt in your case ConsultaCaract()            }        };mainHandler.post(myRunnable);
总结

以上是内存溢出为你收集整理的java – AsyncHttpClient Error中使用的同步ResponseHandler全部内容,希望文章能够帮你解决java – AsyncHttpClient Error中使用的同步ResponseHandler所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存