[英]UnknownHostException in Android App but working in Browser
我知道這個問題已經在SO上問過好幾次了,但是那並不能解決我的問題。
我需要清單文件中的權限。 Web服務在瀏覽器中運行良好,但在Android應用程序中提供了UnknownHostException。
這是我的機器人代碼:
內部艙位
private class WebService extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls){
String res = "";
_callService();
return res;
}
@Override
protected void onPostExecute(String result){
progressDialog.dismiss();
//reset();
}
}
_callService()方法:
public void _callService(){
try{
HttpResponse response = null;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 400000);
HttpConnectionParams.setSoTimeout(httpParameters, 600000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(CommonObject.URL + "CheckUser");
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("Type", String.valueOf(CommonObject.type)));
if(s5.equalsIgnoreCase("Company Account")){
values.add(new BasicNameValuePair("UserType", "2"));
values.add(new BasicNameValuePair("CustomerFirstName", s6));
}else{
values.add(new BasicNameValuePair("UserType", "1"));
values.add(new BasicNameValuePair("CustomerFirstName", s1));
}
values.add(new BasicNameValuePair("CustomerEmail", s2));
values.add(new BasicNameValuePair("CustomerPhoneNo", s3));
values.add(new BasicNameValuePair("CustomerAccountNo", s4));
/* Log of above values*/
Log.v("URL", CommonObject.URL + "CheckUser");
Log.v("Type", String.valueOf(CommonObject.type));
if(s5.equalsIgnoreCase("Company Account")){
Log.v("UserType", "2");
Log.v("CustomerFirstName", s6);
}else{
Log.v("UserType", "1");
Log.v("CustomerFirstName", s1);
}
Log.v("CustomerEmail", s2);
Log.v("CustomerPhoneNo", s3);
Log.v("CustomerAccountNo", s4);
try{
httppost.setEntity(new UrlEncodedFormEntity(values));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("URL Encoded Form Entity Exception", e.getMessage().toString());
}
try{
response = httpclient.execute(httppost);
}catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("Client Protocol Exception", e.getMessage().toString());
}catch (IOException e) {
e.printStackTrace();
Log.e("IO Exception", e.getMessage().toString());
}catch(Exception e){
e.printStackTrace();
Log.e("Exception", e.getMessage().toString());
}
final int statusCode = response.getStatusLine().getStatusCode();
response_code = String.valueOf(response.getFirstHeader("ResponseCode")).trim();
response_message = String.valueOf(response.getLastHeader("Message")).trim();
Log.e("Exact Response From Server", String.valueOf(response));
Log.e("ResponseCode", response_code);
Log.e("Message", response_message);
String content = "";
if(statusCode == 200){
HttpEntity mResEntityGet = response.getEntity();
if (mResEntityGet != null) {
try {
content = EntityUtils.toString(mResEntityGet);
} catch (ParseException e) {
e.printStackTrace();
Log.e("Parse Exception", e.getMessage().toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("IO Exception", e.getMessage().toString());
} catch(Exception e){
e.printStackTrace();
Log.e("Exception", e.getMessage().toString());
}
Log.v("CONTENT", content);
if(response_code.endsWith("200")){
//send_msg = "Mail send";
}else{
//send_msg = "";
}
}
}
}catch(Exception e){
Log.e("Error in _callService", e.getMessage().toString());
}
}
以這種方式在按鈕單擊事件上調用:
WebService service = new WebService();
service.execute();
確認INTERNET權限標簽是否是清單標簽的子級,而不是應用程序標簽的子級。 在其他線程中也出現了類似的錯誤。 Android:UnknownHostException
編輯:在該線程中,如果您閱讀了所有答案,則有多個“可能的解決方案”來解決您的錯誤。
<uses-permission android:name="android.permission.INTERNET" />
<application>
...
</application>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.