我知道这些问题已在SO上多次询问过,但这些问题并没有解决我的问题。
我在Manifest文件中需要权限。 Webservice在浏览器中运行良好,但在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();
答案 0 :(得分:0)
确认INTERNET权限标记是清单标记的子标记而不是应用程序标记的子标记。其他线程中也出现了类似的错误。 Android: UnknownHostException
编辑:在该主题中,如果您阅读了所有答案,则会有多个“可能的解决方案”来解决您的错误。
<uses-permission android:name="android.permission.INTERNET" />
<application>
...
</application>