Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <uses-permission android:name="android.permission.INTERNET" />
- public class PostRequest extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... strings) {
- String address = strings[0];
- String username = strings[1];
- String password = strings[2];
- try {
- URL obj = new URL(address);
- HttpURLConnection con = (HttpURLConnection) obj.openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
- String urlParameters = "username=" + username + "&password=" + password;
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(urlParameters);
- wr.flush();
- wr.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- return response.toString();
- } catch (Exception e) {
- // catch exceptions
- }
- return null;
- }
- }
- String responce = new PostRequest().execute("http://www.example.com/script.php", "username", "password").get();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement