Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BackgroundWorker extends AsyncTask {
- Context context;
- BackgroundWorker (Context context){
- this.context=context;
- }
- @Override
- protected Object doInBackground(Object[] objects) {
- Object type=objects[0];
- String login_url = "http://localhost/android/user.php";
- if(type.equals("login")){
- try{
- String username= (String) objects[1];
- String password=(String)objects[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 bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
- String post_data= URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
- +URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"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(bufferedReader.readLine()!=null){
- result+=line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- }
- catch(MalformedURLException e){
- e.printStackTrace();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment