无法解析主机:URL没有与主机名关联的地址

时间:2013-09-12 14:15:08

标签: android unknown-host androidhttpclient

我有一个Web服务调用来关闭一个JSON对象,它只有在我拥有IP地址而不是主机名时才有效。我使用IP地址很好,但现在我需要拥有主机名。

这是我的代码

    StringBuilder stringBuilder = new StringBuilder();
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
    HttpConnectionParams.setSoTimeout(httpParams, 7000);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpPost httpost = new HttpPost(URL);
    try {
        HashMap<String,String>  params = new HashMap<String,String>();
        // Login info
        params.put("username", edtUserName.getText().toString()); 
        params.put("password", edtPass.getText().toString());
        params.put("deviceOS", "Android");
        params.put("deviceOSVersion", android.os.Build.VERSION.RELEASE);
        params.put("language", "0");
        //JSON object holding the login info
        JSONObject holder = new JSONObject(params);

        StringEntity se = new StringEntity(holder.toString());

        httpost.setEntity(se);

        httpost.setHeader("Content-Type", "application/json");
        //Executing the call
        HttpResponse response = httpClient.execute(httpost);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
        } else {
            returnCode = -1;
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        returnCode = -1;
        Log.d("readJSONFeed", e.getLocalizedMessage());
    }

    return stringBuilder.toString();

我不知道为什么这不起作用。我在使用Wifi连接的手机上进行测试,我的清单包含<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

此网络服务确实有效。只是不在Android上

如果有人有一个很棒的想法,谢谢。

1 个答案:

答案 0 :(得分:6)

我通过删除这些行来修复它

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 7000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);

并用此代码替换

HttpClient httpClient = MainActivity.createHttpClient();


public static HttpClient createHttpClient(){
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 15000);
        HttpConnectionParams.setSoTimeout(params, 5000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}