应用程序访问URL而不显示Android的任何内容
例如,我的android应用程序中有令牌变量。 我正在尝试将此变量发送到服务器并运行查询,因此我将以下url与GET一起使用: 当用户访问此url时,它会将令牌添加到我的数据库中 如何使我的android应用程序在我的应用程序访问URL而不显示Android的任何内容,android,Android,例如,我的android应用程序中有令牌变量。 我正在尝试将此变量发送到服务器并运行查询,因此我将以下url与GET一起使用: 当用户访问此url时,它会将令牌添加到我的数据库中 如何使我的android应用程序在我的受保护的void onPostExecute(字符串令牌)方法上自动访问此URL 这是我到目前为止尝试过的,但不起作用(不要访问URL): toast工作正常,URL未被访问 另一种尝试: protected void onPostExecute(String token) {
受保护的void onPostExecute(字符串令牌)
方法上自动访问此URL
这是我到目前为止尝试过的,但不起作用(不要访问URL):
toast工作正常,URL未被访问
另一种尝试:
protected void onPostExecute(String token) {
URL url;
Toast.makeText(getApplicationContext(),"Works",Toast.LENGTH_LONG).show();
try {
url = new URL("http://example.com/send.php?id="+token);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
日志上写着什么<代码>e.printStackTrace()代码>我没有收到任何错误为什么要尝试在ui线程中进行api调用?请尝试使用HTTPUrlConnection而不是URLConnection,HTTPUrlConnection可以检查响应代码。
protected void onPostExecute(String token) {
URL url;
Toast.makeText(getApplicationContext(),"Works",Toast.LENGTH_LONG).show();
try {
url = new URL("http://example.com/send.php?id="+token);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}