我的应用程序经常向 servlet 发送请求,但从昨天开始它开始给出UnknownHostException Exception。我不知道自 1 个月以来运行良好的代码有什么问题。
我在清单文件中添加了访问互联网的权限。我的 servlet 在 PC 浏览器上运行良好。
我在 Android 中调用 servlet 的代码是:
String url = "http://sampark.iiit.ac.in:8080/VTSServlets/CurrentTripInfo?mobilenum=##########&lat=10&long=10"
String response = getResponse(url);
public static String getResponse(final String url)
{
final StringBuilder builder = new StringBuilder();
final HttpClient client = new DefaultHttpClient();
System.out.println("URL :: " + url);
final HttpGet httpGet = new HttpGet(url);
try {
final HttpResponse response = client.execute(httpGet);
final StatusLine statusLine = response.getStatusLine();
final int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
final HttpEntity entity = response.getEntity();
final InputStream content = entity.getContent();
final BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
builder.append("Not 200");
}
} catch (final ClientProtocolException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
我已经检查了所有要求,但不知道它是如何突然给出 UnknownHost 异常的?我正在 Android 手机上测试我的应用程序。