duanlan6259 2018-12-03 13:38
浏览 72

android -mysql连接使用php错误:java.io.FileNotFoundException

URGENT:I want to connect my android application to mysql using php

but when i run the code it gives me exception java.io.FileNotFoundException

Whenever i run http://192.168.XX.X:8080/login.php through postman it says "Forbidden You dont have permission to access /login.php on this server" but whenever i run http://localhost:8080/login.php it runs fine

"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);
}
}

Any help from your side will be appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题