问题描述
我已经完成了一个 android 应用程序来登录我的数据库并在我的 XAMPP 服务器上注册帐户。 此应用程序在我的 PC 和手机上运行良好,但仅当我处于 WiFi 连接下时。 当我切换到移动连接时(我的情况是 4g)在我的智能手机上不起作用。
系统捕捉到一个 IOException 并在一个对话框中写入我只配置了 PHP 文件的 url,例如“http://EXAMPLEIP/login.PHP”。
有人知道是不是配置问题? 不要介意EXAMPLEIP,在我的代码中有从ipconfig 命令获取的正确IP。
我使用 AsyncTask 处理连接:
@Override
protected String doInBackground(String... voids) {
code = voids[0];
username = voids[1];
password = voids[2];
if(code==LOGIN_CODE){
web_address = "EXAMPLEIP/login.PHP";
}else if(code==REGISTER_CODE){
web_address = "EXAMPLEIP/register.PHP";
bestPlace = voids[3];
}
try{
URL url = new URL(web_address);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getoutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
if(code==LOGIN_CODE){
data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")
+"&&"+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
}else if(code==REGISTER_CODE){
data = URLEncoder.encode("username","UTF-8")
+"&&"+URLEncoder.encode("bestPlace","UTF-8")+"="+URLEncoder.encode(bestPlace,"UTF-8");
}
writer.write(data);
writer.flush();
writer.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"ISO-8859-1"));
String line ="";
while((line = reader.readLine())!=null){
result += line;
}
reader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch (MalformedURLException e){
result = e.getMessage();
}catch (IOException e){
result = e.getMessage();
}
return result;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)