我在adroid中创建了一个应用程序,假设我根据特定的时间表从我放在我的web服务上的json下载一些数据。我将AlarmManager设置为fire service,其中包含负责连接到Internet并下载json的异步任务。奇怪的是,当我调试应用程序时,一切正常,但是当我在调试模式下运行时,我得到:
java.net.UnknownHostException:无法解析主机"
<host address of my web app>
&#34;:没有与主机名关联的地址
你能告诉我为什么一切都在我调试时才能正常工作吗?
修改
在这种方法中,我称之为异步任务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
dataSwitch.setMobileDataEnabled(true, this);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
/*GETTING PATIENT ID AND SCHEDULE FORM SHARED PREFERENCES*/
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
prefs = PreferenceManager.getDefaultSharedPreferences(this);
editor = prefs.edit();
patientId = prefs.getLong("patientId", -1);
schedule = prefs.getString("schedule", null);
numberOfEvents = prefs.getInt("numberOfEvents", -1);
credentials = prefs.getString("credentials", null);
/* CHECKING IF PATIENT ID IS SET*/
if (patientId != -1 && credentials!=null) {
isLoginCorrect = true;
/*STARTING ASYNC TASK*/
new CheckUpdateAsync().execute();
} else {
Toast.makeText(context, "NO PATIENT SELECTED", Toast.LENGTH_SHORT).show();
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
这是我打开连接的异步任务的一部分:
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected ListOfPatients doInBackground(Void... params) {
HttpURLConnection connection;
ListOfPatients patients;
final String url = getString(R.string.url_of_webapp);
try {
/*LOGIN USING HTTP URL CONNECTION*/
connection = (HttpURLConnection) new URL(url + "j_spring_security_check").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream output = connection.getOutputStream()) {
output.write(credentials.getBytes(charset));
InputStream input = connection.getInputStream();
/*CHECKING IF STARTING PAGE WAS LOADED*/
if (!connection.getURL().toString().equals(url + "user/start")) {
isLoginCorrect = false;
}
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
EDIT2 这是我的gradle构建:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/LGPL2.1'
}
defaultConfig {
applicationId "com.example.szymon.vibrationmeasureservice"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.0'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.6.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
}
repositories {
maven {
url 'http://repo.spring.io/milestone'
}
}