本文介绍了无法解析主机:URL没有与主机名关联的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Web服务调用打倒一个JSON对象,如果我的IP地址,而不是主机名它仅适用。我一直罚款使用IP地址,但现在我需要有主机名。
I have an web service call to bring down a JSON object and it only works if I have the IP address and not the host name. I have been fine using IP addresses but now I need to have the host name.
下面是我的code
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连接在手机上测试,我的清单有&LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT;
和&LT;使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/&GT;
本网站服务不工作。只是没有在Android
This web service does work. Just not on Android
如果任何人有一个想法,将是巨大的,谢谢你。
If anyone has an idea that would be great, Thanks.
推荐答案
我通过删除这些行固定它
I fixed it by removing these lines
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 7000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
而与此code替换它。
And replacing it with this code
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();
}
}
这篇关于无法解析主机:URL没有与主机名关联的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!