2017-02-15 8 views
0

Wenn ich hart codieren kann ich mich anmelden, aber ich möchte es generische Weise machen, wenn Anmeldeinformationen in Login-Bildschirm eingeben, wenn es von Service übereinstimmt es Login.Ich verwende auf diese WeiseIch möchte Anmeldeinformationen in generischer Weise machen

public class LoginScreen extends Activity { 

private static final String TAG = "Login Screen"; 

public static final int CONNECTION_TIMEOUT=100000; 
public static final int READ_TIMEOUT=15000; 

private EditText editTextUsername; 
private EditText editTextPassword; 


String username; 
String password; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login_screen); 

    editTextUsername = (EditText)findViewById(R.id.emaiId); 
    editTextPassword = (EditText)findViewById(R.id.password); 

} 

public void buttonLogin (View arg0) 
{ 
    username = editTextUsername.getText().toString(); 
     password = editTextPassword.getText().toString(); 
    new TaskLogin().execute(username,password); 
} 

public class TaskLogin extends AsyncTask<String,String,String> 
{ 
    ProgressDialog pdLoading = new ProgressDialog(LoginScreen.this); 
    HttpURLConnection conn; 
    URL url = null; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pdLoading.setMessage("\t Loading..."); 
     pdLoading.setCancelable(false); 
     pdLoading.show(); 
    } 
    @Override 
    protected String doInBackground(String... params) { 

     StringBuilder result = new StringBuilder("unsuccessful"); 
     try{ 
      url = new URL(URLConstants.getLoginURL()); 

      conn = (HttpURLConnection)url.openConnection(); 
      conn.setReadTimeout(READ_TIMEOUT); 
      conn.setConnectTimeout(CONNECTION_TIMEOUT); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Content-Type","application/json"); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      OutputStream os = conn.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8")); 

      writer.write("{\"email\":\"[email protected]\",\"password\":\"12345\"}"); 
      writer.flush(); 
      writer.close(); 


      int response_code = conn.getResponseCode(); 
      System.out.print("Result"+ response_code); 
      BufferedReader reader = null; 

      if(response_code==HttpURLConnection.HTTP_OK) 
      { 
       InputStream input = conn.getInputStream(); 
       reader = new BufferedReader(new InputStreamReader(input)); 
       result = new StringBuilder(); 
       String line; 
       while ((line= reader.readLine())!=null) 
       { 
        result.append(line); 
       } 
       System.out.println("result "+result); 
      } 

      reader.close(); 
      os.close(); 
      conn.disconnect(); 
     } catch (Exception e1) 
     { 
      e1.printStackTrace(); 
      return "Exception"; 
     } 
     return result.toString(); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     pdLoading.dismiss(); 
     if(result.equalsIgnoreCase("true")) 
     { 
      Intent intent = new Intent(LoginScreen.this,Main2Activity.class); 
      startActivity(intent); 
      LoginScreen.this.finish(); 
     }else if (result.equalsIgnoreCase("false")) 
     { 
      Toast.makeText(LoginScreen.this, "Invalid email or password", Toast.LENGTH_LONG).show(); 
     } else if(result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) 
     { 
      Toast.makeText(LoginScreen.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

}

Meine URL von Benutzername und Passwort bestehen

{"email":"[email protected]","password":"123"} 
+0

Hallo und was ist die Frage? – Gwaeron

+0

In obigen Code bin ich hart in BufferedWriter mit E-Mail und Passwort codieren, dass ich es in generischer Weise machen will. Bitte, wie man es macht –

+0

so ist die Frage, "wie man Text in meine App eingibt", richtig? Sie können einige 'EditText's für diese –

Antwort

0
Use JsonObject like below: 

JSONObject job=new JSONObject(); 
     job.put("email",emailId); 
     job.put("pwd",pwd); 
writer.write(job.tostring()); 

wo emailId & pwd sind Variable

+0

sein nicht arbeitender Typ –

Verwandte Themen