【发布时间】:2020-06-19 02:28:40
【问题描述】:
我有一个 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 上
如果有人有很棒的想法,谢谢。
【问题讨论】:
-
您可以在手机的网络浏览器中点击地址(主机名)吗?
-
我试过了,在 iPhone 上它可以正常进入页面,但 android 给我一个“网页不可用”
-
您必须在本地网络上的内网地址?
-
你发现的异常是什么?
-
@IgorGanapolsky 如果我没记错的话,我相信我捕获的异常是 SSL 异常
标签: android unknown-host androidhttpclient