protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://192.168.43.230:25689/test/login.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username", params[0])
.appendQueryParameter("password", params[1]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
return e1.getMessage();
//return "exception";
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return(result.toString());
}else{
return Integer.toString(response_code);
//return("unsuccessful");
}
我正在尝试从我手机上的 android 应用程序运行 php 脚本(wamp 服务器)。我将手机和电脑连接到同一网络。
响应代码显示 403。我禁用了防火墙并添加了用户代理。
任何人都可以告诉错误的可能性是什么。
最佳答案
那么,由于 HTTP 响应代码是 403 意味着访问被拒绝,这意味着问题很可能在 Web 服务器端。您可以检查的几件事是网络服务器是否具有检索您尝试访问的文件的访问/权限。
同时检查您的 login.php 的路由是否配置为可在您的网络服务器配置中访问。
关于php - 在通过 Android 应用程序访问本地主机中的 PHP 脚本时,我在 Connection{192.168.43.230 :25689, ..},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42586223/