有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android应用程序无法连接到php脚本

我试图将数据发送到我的php脚本并读取回复。然而,代码直接指向“catch”

public class Login extends AppCompatActivity   {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Button login = (Button) findViewById(R.id.button_login);
       //EditText username = (EditText) findViewById(R.id.editxt_username);
       //EditText password = (EditText) findViewById(R.id.editxt_password);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {



                        EditText username = (EditText) findViewById(R.id.editxt_username);
                        EditText password = (EditText) findViewById(R.id.editxt_password);

                        final String   mUsername = username.getText().toString();
                        final String  mPassword = password.getText().toString();



                            tryLogin(mUsername, mPassword);



                //Intent intent = new Intent(getApplicationContext(), Home.class);
                //startActivity(intent);
            }
        });
    }

    protected void tryLogin(String mUsername, String mPassword)
    {
        HttpURLConnection connection;
        OutputStreamWriter request = null;

        URL url = null;
        String response = null;
        String parameters = "KORISNICKO_IME="+mUsername+"&LOZINKA="+mPassword;

        try
        {

            url = new URL("link");
            connection =(HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");
            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();
            String line = "";
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            // Response from server after login process will be stored in response variable.
            response = sb.toString();
            // You can perform UI operations here
            Toast.makeText(this,"Message from Server: \n"+ response, Toast.LENGTH_LONG).show();
            isr.close();
            reader.close();

        }
        catch(IOException e)
        {
            Toast.makeText(this,"Message from Server: \n", Toast.LENGTH_LONG).show();

        }
    }
}

知道我做错了什么吗


共 (0) 个答案