简体   繁体   中英

UnknownHostException in Android App but working in Browser

I know this questions has been asked several times on SO but those doesn't resolve my problem.

I have required permissions in Manifest file. Webservice is working fine in browser but gives UnknownHostException in Android application.

Here is my droid code:

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();
            }
        }

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();

Confirm if the INTERNET permission tag is a child of the manifest tag and not of the application tag. A similar error was presented in other thread. Android: UnknownHostException

EDIT: In that thread if you read all answers there are multiple "possible solutions" for your error.

 <uses-permission android:name="android.permission.INTERNET" />
 <application>
 ...
 </application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM