简体   繁体   中英

error httpclient run without thread

Code:

public boolean CheckDeviceID(final Context context){
    try {
        //Looper.prepare(); //For Preparing Message Pool for the child Thread
        HttpClient httpclient;
        HttpPost httppost;
        ArrayList<NameValuePair> postParameters;
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://"+Server+"/BusTicket/checkdevice/checkdevice.php");

        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber().substring(0, tm.getSimSerialNumber().length()-1);
        Log.d(null,"DeviceID="+tmSerial);
        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("DeviceID", tmSerial));
        httppost.setEntity(new UrlEncodedFormEntity(postParameters));
        HttpResponse response = httpclient.execute(httppost);
        InputStream in = response.getEntity().getContent(); //Get the data in the entity
        String jsonstring = convertStreamToString(in);
        Log.d(null,"CheckDevice Reponse= "+ jsonstring);
        jsonstring = jsonstring.trim();
        if (jsonstring.equalsIgnoreCase("valid")){
            return true;
        }else{
            return false;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d(null,"CheckDevice error= "+ e.getMessage());
    }
    //Looper.loop(); //Loop in the message queue
    return false;
}

I have BroadcastReceiver and set alarm with every 10mins call this CheckDevice function, Check whether this device are registered in my database or not, if this function run in a thread is working, but can't return boolean with thread?!?

but if i run in OnReceive with this function, will cause the error, im trying to print the error with e.getMessage and returned error with null

any idea run the httpclient without thread? or let the thread return boolean ?

Update

im trying to use AsynTask to get the return boolean, and this code are inside OnReceive

new AsyncTask<Void, Void, Boolean>()
                {
                    @Override
                    protected Boolean doInBackground(Void... p)
                    {
                        return CheckDeviceID(context);
                    }

                    @Override
                    protected void onPostExecute(Boolean result)
                    {
                        //this is code for the UI thread, now that it knows what is the result.
                    }
                }.execute();

But, in this code, how to get the return boolean?

Two rules you must know about: 1- You must do any network operation in a different thread. 2- Don't put code that takes long time inside onReceive because receivers has small lifetime

You won't be able to return boolean but I think, among other options, you can use SharedPreferences or files to save the result of the thread and check this SharedPreferences value inside onReceive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM