简体   繁体   English

Android执行不同的http请求卡住

[英]Android perform different http requests stuck

I have the following code: 我有以下代码:

// Activate every time UART Characteristic is updated
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);

    final String[] data = new String(characteristic.getValue()).split("/");

    String endpoint = "http://192.168.1.100:8000/rest/store/"+gatt.getDevice().getAddress()+"/"+data[0]+"/"+data[1]+"/"+data[2]+"/"+Long.toString(System.currentTimeMillis());
    try { new uploadData().execute(new URL(endpoint)); }
    catch (MalformedURLException e) { e.printStackTrace(); }

    String endpoint2 = "http://192.168.1.100:8000/rest/status/"+gatt.getDevice().getAddress();
    try { new checkServer().execute(new URL(endpoint2)); }
    catch (MalformedURLException e) { e.printStackTrace(); }  
}

Every time the accelerometer updates its values, I receive a notification that activates onCharacteristicChanged . 每次加速度计更新其值时,我都会收到激活onCharacteristicChanged的通知。

uploadData , is an async task that tries to upload the accelerometer information to my server. uploadData是一个异步任务,尝试将加速度计信息上载到我的服务器。 It doesn't have timeout because it has to wait until the server is online to not lose information. 它没有超时,因为它必须等到服务器联机后才能丢失信息。

checkServer is an async task that checks the server health, and display a message on android if it is online or not. checkServer是一个异步任务,用于检查服务器运行状况,并在android上显示是否在线(无论是否在线)。 It has a 500ms timeout to show that the server is offline. 它具有500毫秒的超时时间,以表明服务器处于脱机状态。

Here is the code: 这是代码:

private class uploadData extends AsyncTask<URL, Integer, String> {

    protected String doInBackground(URL... urls) {
        try {
            HttpURLConnection urlConnection = (HttpURLConnection) urls[0].openConnection();
            urlConnection.getResponseMessage();
            urlConnection.disconnect();
        }
        catch (IOException e) { }
        return null;
    }
}

private class checkServer extends AsyncTask<URL, Integer, String> {

    String result;

    protected String doInBackground(URL... urls) {
        try {
            HttpURLConnection urlConnection = (HttpURLConnection) urls[0].openConnection();
            urlConnection.setConnectTimeout(500);
            result = urlConnection.getResponseMessage();
            urlConnection.disconnect();
        }
        catch (IOException e) { result = "404"; }
        return result;
    }

    protected void onPostExecute(String result) {
        if (result.equalsIgnoreCase("OK")) {
            ((TextView) findViewById(R.id.http)).setText("Sending data");
            ((TextView) findViewById(R.id.http)).setTextColor(Color.GREEN);
        }
        else{
            ((TextView) findViewById(R.id.http)).setText("Server offline");
            ((TextView) findViewById(R.id.http)).setTextColor(Color.RED);
        }
    }
}

When server is online, everything is ok. 服务器联机时,一切正常。 It sends the information and it shows that the server is online. 它发送信息,并显示服务器处于联机状态。

But when the server is offline, it gets stuck with the message: Sending data . 但是,当服务器处于脱机状态时,它会陷入以下消息: Sending data

Why it gets stuck? 为什么会卡住? It seems that he's waiting until uploadData finishes, but it doesn't have a timeout so it never executes until server is online again. 似乎他正在等待直到uploadData完成,但是它没有超时,因此它永远不会执行,直到服务器再次联机。 Doesn't Android execute all http request at the same time asynchronous? Android不会同时异步执行所有http请求吗?

尝试在onPostExecute方法上添加一条语句:

if (result == "404")`{add a dispplay msg here}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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