Android: How to programatically login to website and retrieve data from it?

后端 未结 2 1400
南笙
南笙 2020-12-14 13:24

Ok, guys, so I\'m recently developing android app that saves user\'s ID and PASSWORD to SharedPreferences. Now, when the user starts app second time, he will be redirected d

2条回答
  •  时光说笑
    2020-12-14 14:13

    if you want to login with the you application in java or in android then you need to try with HTTPPOST

    example code:

    public class HttpPostExample extends Activity {
    
          TextView content;
          EditText fname, email, login, pass;
          String Name, Email, Login, Pass;
    
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_http_post_example);
    
              content    =   (TextView)findViewById( R.id.content );
              fname      =   (EditText)findViewById(R.id.name);
              email      =   (EditText)findViewById(R.id.email);
              login      =    (EditText)findViewById(R.id.loginname);
              pass       =   (EditText)findViewById(R.id.password);
    
    
              Button saveme=(Button)findViewById(R.id.save);
    
              saveme.setOnClickListener(new Button.OnClickListener(){
    
                  public void onClick(View v)
                  {
                      try{
    
                               // CALL GetText method to make post method call
                              GetText();
                       }
                      catch(Exception ex)
                       {
                          content.setText(" url exeption! " );
                       }
                  }
              }); 
          }
    
      // Create GetText Metod
    public  void  GetText()  throws  UnsupportedEncodingException
          {
              // Get user defined values
              Name = fname.getText().toString();
              Email   = email.getText().toString();
              Login   = login.getText().toString();
              Pass   = pass.getText().toString();
    
               // Create data variable for sent values to server 
    
                String data = URLEncoder.encode("name", "UTF-8")
                             + "=" + URLEncoder.encode(Name, "UTF-8");
    
                data += "&" + URLEncoder.encode("email", "UTF-8") + "="
                            + URLEncoder.encode(Email, "UTF-8");
    
                data += "&" + URLEncoder.encode("user", "UTF-8")
                            + "=" + URLEncoder.encode(Login, "UTF-8");
    
                data += "&" + URLEncoder.encode("pass", "UTF-8")
                            + "=" + URLEncoder.encode(Pass, "UTF-8");
    
                String text = "";
                BufferedReader reader=null;
    
                // Send data
              try
              {
    
                  // Defined URL  where to send data
                  URL url = new URL("http://androidexample.com/media/webservice/httppost.php");
    
               // Send POST data request
    
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write( data );
                wr.flush();
    
                // Get the server response
    
              reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              StringBuilder sb = new StringBuilder();
              String line = null;
    
              // Read Server Response
              while((line = reader.readLine()) != null)
                  {
                         // Append server response in string
                         sb.append(line + "\n");
                  }
    
    
                  text = sb.toString();
              }
              catch(Exception ex)
              {
    
              }
              finally
              {
                  try
                  {
    
                      reader.close();
                  }
    
                  catch(Exception ex) {}
              }
    
              // Show response on activity
              content.setText( text  );
    
          }
    
      }
    

    i am getting this source from this url in this url you get all thing related your query.

    or If you want to connect to the *FTP Login * then you can follow bellow code

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.SocketException;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    public class Ftpdemo {  
    
    
         public static void main(String args[]) {  
    
          // get an ftpClient object  
          FTPClient ftpClient = new FTPClient();  
          ftpClient.setConnectTimeout(300);
          FileInputStream inputStream = null;  
    
          try {  
           // pass directory path on server to connect  
           ftpClient.connect("ftp.mydomain.in");  
    
           // pass username and password, returned true if authentication is  
           // successful  
           boolean login = ftpClient.login("myusername", "mypassword");  
    
           if (login) {  
            System.out.println("Connection established...");  
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    
            ftpClient.enterLocalPassiveMode();
    
            inputStream = new FileInputStream("/home/simmant/Desktop/mypic.png");  
    
            boolean uploaded = ftpClient.storeFile("user_screens/test3.png",inputStream);
    
    
    
                  if (uploaded) {  
             System.out.println("File uploaded successfully !");  
            } else {  
             System.out.println("Error in uploading file !");  
            }  
    
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();  
            if (logout) {  
             System.out.println("Connection close...");  
            }  
           } else {  
            System.out.println("Connection fail...");  
           }  
    
          } catch (SocketException e) {  
           e.printStackTrace();  
          } catch (IOException e) {  
           e.printStackTrace();  
          } finally {  
           try {  
            ftpClient.disconnect();  
           } catch (IOException e) {  
            e.printStackTrace();  
           }  
          }  
         }
    
    
        }
    

提交回复
热议问题