Sending post to PHP from Android app

Refresh

November 2018

Views

39 time

2

I am trying to send data from an android application to PHP using POST the PHP will forward it to MySQL database and give respond. the application is crashing. can somebody please assist me.

@Override
public void onClick(View view) {
    try {
        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 bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("p", "UTF-8") + "=" + URLEncoder.encode(p, "UTF-8") + "&" +
            URLEncoder.encode("q", "UTF-8") + "=" + URLEncoder.encode(q, "UTF-8") + "&" +
            URLEncoder.encode("r", "UTF-8") + "=" + URLEncoder.encode(r, "UTF-8") + "&" +
            URLEncoder.encode("s", "UTF-8") + "=" + URLEncoder.encode(s, "UTF-8");
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        InputStream inputstream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
        String result = "";
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        bufferedReader.close();
        inputstream.close();
        httpURLConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 answers

0

Проверьте нить !!

Вы запустите запрос HTTP на UI потоке, поэтому вы получите сообщение об ошибке.

Попробуйте создать новую тему

new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                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 bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("p","UTF-8")+"="+URLEncoder.encode(p,"UTF-8")+"&"
                        +URLEncoder.encode("q","UTF-8")+"="+URLEncoder.encode(q,"UTF-8")+"&"
                        +URLEncoder.encode("r","UTF-8")+"="+URLEncoder.encode(r,"UTF-8")+"&"
                        +URLEncoder.encode("s","UTF-8")+"="+URLEncoder.encode(s,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputstream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
                String result = "";
                String line = "";
                while((line = bufferedReader.readLine()) != null){
                    result += line;
                }
                bufferedReader.close();
                inputstream.close();
                httpURLConnection.disconnect();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();