Android Login mit Async

  • 1 Antworten
  • Letztes Antwortdatum
D

Didi95

Neues Mitglied
0
Hallo liebe Community,
ich muss momentan eine App für die Schule schreiben. Wir haben dort ein LDAP System, indem die Logindaten eines jeden Schülers stehen. Ich hab eine entsprechende Api geschrieben, die einfach nur mit {success:1} prüft, ob der Schüler vorhanden ist. Nun habe ich mich an die Programmierung der Loginactivity gemacht. Und da fangen die Probleme an. Dadurch, dass man ab Android 4.0 den Asynctask braucht um eine Internetverbindung herzustellen weiß ich nicht genau, wie ich das richtig anstellen soll. Zudem muss ich mit der POST Methode ja auch Daten an den Server übergeben. Ich würde jetzt ganz gerne von euch wissen, ob ich mit dem unten angehängten Code auf dem richtigen oder auf dem Holzweg bin. Wenn ich total falsch liege bitte ich um Codebeispiele oder Denkanstöße, da ich einfach nicht weiß wie ich das anders regeln soll. :confused2:

Viele Dank schonmal :cool2:
Didi95

Code:
public class Login extends Activity{
	

	Button Login;
	EditText InputName;
	EditText InputPassword;
	private ProgressDialog pDialog;
	
	// Creating JSON Parser object
	JSONParser jParser = new JSONParser();
	
	private static String url_login = "http://Plus.eichsfeld-gymnasium.de/App/Login.php ";
	
	private static String KEY_SUCCESS = "success";
	private static String KEY_Name = "username";
	private static String KEY_PASSWORT = "password";
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		// Buttons
				Login = (Button) findViewById(R.id.Login);
				InputName = (EditText) findViewById(R.id.editText1);
				InputPassword = (EditText) findViewById(R.id.editText2);
				
				
				
				//Load Login into a Background Task making HTTP request
				
				// view products click event
				Login.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View view) {
						String Name = InputName.getText().toString();
						String password = InputPassword.getText().toString();
						
					 new LoadAllLogin().execute();
						
						class LoadAllLogin extends AsyncTask<String, String, String> {
							
							
							protected void onPreExecute() {
								super.onPreExecute();
								pDialog = new ProgressDialog(Login.this);
								pDialog.setMessage("Login bitte warten...");
								pDialog.setIndeterminate(false);
								pDialog.setCancelable(true);
								pDialog.show();
							}
							
							
							@Override
							protected String doInBackground(String... args) {
								List<NameValuePair> params = new ArrayList<NameValuePair>();
								// TODO Auto-generated method stub
								JSONObject json = jParser.makeHttpRequest(url_login, "POST", params);
								try {
									// Checking for SUCCESS TAG
									int success = json.getInt(KEY_SUCCESS);
									

									if (success == 1) {
										Intent MainActivity = new Intent(getApplicationContext(), MainActivity.class);
										
										// Close all views before launching MainActivity
										MainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										startActivity(MainActivity);
										
										// Close Login Screen
										finish();
									}else{
										// Error in login
										
									}
								}
							 catch (JSONException e) {
								e.printStackTrace();
							}
									
								return null;
							}
						
					
						
					}
					}
				});

}
}


JSON Parser:

Code:
public class JSONParser {

	static InputStream is = null;
	static JSONObject jObj = null;
	static String json = "";

	// constructor
	public JSONParser() {

	}

	// function get json from url
	// by making HTTP POST or GET mehtod
	public JSONObject makeHttpRequest(String url, String method,
			List<NameValuePair> params) {

		// Making HTTP request
		try {
			
			// check for request method
			if(method == "POST"){
				// request method is POST
				// defaultHttpClient
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new UrlEncodedFormEntity(params));

				HttpResponse httpResponse = httpClient.execute(httpPost);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();
				
			}else if(method == "GET"){
				// request method is GET
				DefaultHttpClient httpClient = new DefaultHttpClient();
				String paramString = URLEncodedUtils.format(params, "utf-8");
				url += "?" + paramString;
				HttpGet httpGet = new HttpGet(url);

				HttpResponse httpResponse = httpClient.execute(httpGet);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();
			}			
			

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
		} catch (Exception e) {
			Log.e("Buffer Error", "Error converting result " + e.toString());
		}

		// try parse the string to a JSON object
		try {
			jObj = new JSONObject(json);
		} catch (JSONException e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
		}

		// return JSON String
		return jObj;

	}
}
 
Niemand eine Idee? :/
 

Ähnliche Themen

Katharina1985
Antworten
9
Aufrufe
334
Anz
Anz
M
Antworten
21
Aufrufe
1.550
swa00
swa00
Mr-Fisch
Antworten
5
Aufrufe
1.070
migi01
migi01
Mr-Fisch
Antworten
8
Aufrufe
1.094
Mr-Fisch
Mr-Fisch
M
Antworten
9
Aufrufe
915
mkuz24
M
Zurück
Oben Unten