Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConnectionRequest extends AsyncTask<Void, Void, String> {
- private HttpURLConnection conn = null;
- @Override
- protected String doInBackground(Void... params) {
- String address = "www.example.co.il/login.php" , attr = "username=moshe&password=123";
- String answer;
- OutputStreamWriter request = null;
- try {
- URL url = new URL(address);
- conn = (HttpURLConnection) *****openConnection();
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestMethod("POST");
- request = new OutputStreamWriter(conn.getOutputStream());
- request.write(attr);
- request.flush();
- request.close();
- BufferedReader in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- String inputLine;
- StringBuilder sb = new StringBuilder();
- while ((inputLine = in.readLine()) != null)
- sb.append(inputLine);
- in.close();
- answer = sb.toString();
- } catch (IOException e) {
- } finally {
- if (conn != null)
- conn.disconnect();
- }
- return answer;
- }
- @Override
- protected void onPostExecute(String params) {
- String answer = params;
- if(answer.equals("true"))
- Toast.makeText(context, "username exists", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(context, "username not exists", Toast.LENGTH_SHORT).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement