在我的应用程序中,我试图使用以下代码点击我执行的URL
try {
url = new URL(serverURL);
httpURLConnection = (HttpURLConnection) url.openConnection();
int timeout = 30000;
httpURLConnection.setConnectTimeout(timeout);
httpURLConnection.setReadTimeout(timeout);
httpURLConnection.connect();
String httpResponseMessage = httpURLConnection.getResponseMessage();
responseCode = httpURLConnection.getResponseCode();
Log.i(LOG_TAG,"Response code "+responseCode);
} catch (Exception e) {
e.printStackTrace();
}
通过浏览器(在计算机和手机上)打开时,(机密)URL工作正常,响应符合预期。但是当我通过上面的代码点击相同的URL时,它会给我响应代码404(未找到)。谁能告诉我这个问题是什么? (抱歉,由于高度机密,无法发布网址。)
答案 0 :(得分:1)
您确定AndroidManifext.xml
已声明android.permission.INTERNET吗?
答案 1 :(得分:1)
问题解决了:))
try {
url = new URL(serverURL);
Log.i(LOG_TAG, url+"");
HttpGet method= new HttpGet(new URI(serverURL));
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(serverURL));
HttpResponse response = client.execute(method);
responseCode = response.getStatusLine().getStatusCode();
Log.i(LOG_TAG,"Response code response "+response);
Log.i(LOG_TAG,"Response responseCode "+responseCode);
} catch (Exception e) {
e.printStackTrace();
}
答案 2 :(得分:0)
实际上,您甚至不需要在代码中使用两行代码。
HttpGet request = new HttpGet();
request.setURI(new URI(serverURL));
一个HttpGet就够了,你不需要它两次。
答案 3 :(得分:0)
不确定这是否重要,但我遇到了确切的问题。
我正在做一些明确的端口80的东西并删除这一行使它工作:
HttpHost host = new HttpHost(targetHost, 80, "http");