Create sync version of request
Этот коммит содержится в:
@@ -41,27 +41,7 @@ public class RequestManager {
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... request) {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
URL url = new URL(request[0]);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setReadTimeout(TIMEOUT);
|
||||
connection.setConnectTimeout(TIMEOUT);
|
||||
connection.connect();
|
||||
inputStream = connection.getInputStream();
|
||||
while (inputStream.read() != -1);
|
||||
return true;
|
||||
} catch (IOException error) {
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException secondError) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return sendRequest(request[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,7 +54,31 @@ public class RequestManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRequest(String request, RequestHandler handler) {
|
||||
public static boolean sendRequest(String request) {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
URL url = new URL(request);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setReadTimeout(TIMEOUT);
|
||||
connection.setConnectTimeout(TIMEOUT);
|
||||
connection.connect();
|
||||
inputStream = connection.getInputStream();
|
||||
while (inputStream.read() != -1);
|
||||
return true;
|
||||
} catch (IOException error) {
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException secondError) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRequestAsync(String request, RequestHandler handler) {
|
||||
RequestAsyncTask task = new RequestAsyncTask(handler);
|
||||
task.execute(request);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class TrackingController implements PositionProvider.PositionListener {
|
||||
Integer.parseInt(preferences.getString(MainActivity.KEY_PORT, null)),
|
||||
position);
|
||||
|
||||
RequestManager.sendRequest(request, new RequestManager.RequestHandler() {
|
||||
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
|
||||
package org.traccar.client;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.lang.Override;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21)
|
||||
@@ -17,16 +15,7 @@ public class RequestManagerTest {
|
||||
@Test
|
||||
public void testSendRequest() throws Exception {
|
||||
|
||||
RequestManager.sendRequest("http://www.google.com", new RequestManager.RequestHandler() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
});
|
||||
assertTrue(RequestManager.sendRequest("http://www.google.com"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user