紧急:我想使用php将我的android应用程序连接到mysql
但是当我运行代码时,它给了我异常
java.io.FileNotFoundException
每当我通过邮递员运行http://192.168.XX.X:8080/login.php时,都会说 “禁止 您无权访问此服务器上的/login.php” 但是只要我运行http://localhost:8080/login.php,它就可以正常运行
“ MainActivity.java”
public class MainActivity extends AppCompatActivity {
Button btn;
EditText username,password;
String type="Login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.mLogin);
username=findViewById(R.id.mUser);
password=findViewById(R.id.mPass);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Username=username.getText().toString();
String Password=password.getText().toString();
BackgroundWorker bw=new
BackgroundWorker(getApplicationContext());
bw.execute(type,Username,Password);
}
});
}
}
“ BackgroundWorker.java”
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
// AlertDialog ad;
boolean b;
BackgroundWorker(Context context){
this.context=context;
}
@Override
protected String doInBackground(String... strings) {
String type=strings[0];
String login_url="http://192.168.XX.X:8080/login.php";
try {
String user_name=strings[1];
String password=strings[2];
URL url=new URL(login_url);
HttpURLConnection httpURLConnection=
(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream =httpURLConnection.getOutputStream();
BufferedWriter bw=new BufferedWriter(new
OutputStreamWriter(outputStream,"UTF-8"));
String post_data= URLEncoder.encode("user_name","UTF-
8")+"="+URLEncoder.encode(user_name,"UTF-8")
+"&"+URLEncoder.encode("password","UTF-
8")+"="+URLEncoder.encode(password,"UTF-8");
bw.write(post_data);
bw.flush();
bw.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader br=new BufferedReader(new
InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line=br.readLine())!=null)
{
result+=line;
}
System.out.println("here result=="+result);
br.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(type.equals("Login"))
{
}
return null;
}
@Override
protected void onPreExecute() {
// ad=new AlertDialog.Builder(this.context).create();
// ad.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
System.out.println("result=="+result);
if(result.equals("Login successful")){
Toast.makeText(context,"Login success",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"Login failure",Toast.LENGTH_SHORT).show();
}
//ad.setMessage(result);
//ad.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
您身边的任何帮助将不胜感激!