What I try to do
Hello Guys, I’m trying to create a Login-Screen which is connectet to my server. For this I created a Method which is in a other pakage than my Activity. I use this Method in my Activty and it works like a charme.
Now the Problem starts, when I try to pass a Intent back from my Method (Thread) it isn’t possible. How can realize that?
For this I also searched Stackoverflow first and found this post: pritty much the same problem but I don’t get how this should work so I ask again.
Question
So to my question. I’d like to know how can I pass an Intent from my Thread to my Activity. I’d like to do this really simple. If you have some Sample’s or a good tutorial, I please post it in the awnsers!
Down here i’ll provide you the code you need to get what i’d like to do.
Code
Datahandler
package de.ivocore.service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import de.ivocore.LoginActivity;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;
public class DataHandler {
/** LOGIN-Methode
* Für diese Methode muss ein "final String username" sowie ein "final String password" mitgegeben werden
* Die Methode fürt die Login-Prozedur mit dem api.i-v-o.ch durch.
* Zurückgeben wird die Response welche vom Server kommt (Login erfolgreich oder nicht), sowie der Sessionkey.
* */
public void JSONLogin(final String username, final String password){
Thread t = new Thread(){ //Neuen Thread definieren
public void run(){ //Startet automatisch wenn der Thread gestartet wird, führt die Funktion aus
String URL = "CENCORED"; //URL definieren für JSON-Post
DefaultHttpClient client = new DefaultHttpClient(); //HttpClient definieren (DefaultHttpClient(); ist speziel optimiert für Android, darin ist auch der Timeout definiert)
HttpResponse response; //HttpResponse definieren, handelt den http response
JSONObject login = new JSONObject(); //JSON-Object welches gesendet wird
try{
HttpPost post = new HttpPost(URL); //HttpPost definieren, handelt den http post
login.put("username", username); //Füget dem JSONObject Login den Username ein
login.put("password", password); //Fügt dem JSONObject Login das Passwort ein
String test = login.toString(); Log.d("JSON", test);
StringEntity se = new StringEntity("json="+login.toString());
post.setEntity(se);
//post.setHeader("Accept", "application/json");
//hkgvjpost.setHeader("Content-type", "application/json");
response = client.execute(post);
/** Response-Checker
* Hier wird gecheckt was wir in der Response bekommen
*/
if(response != null){ //Hier wird geprüft ob etwas in der Response ist
StringBuilder sb = new StringBuilder(); //Stringbuilder um aus dem Response einen String zu erstellen
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); //response vom inputstreamreader lesen und in einen bufferedreader einfügen um diesen anschliessend zu einem string hinzuzuzfügen und in ein json object umwandeln
String line; //String definieren darin ist die später die response
while ((line = rd.readLine()) != null) { //Hier wird definiert das der String Line den inhalt vom bufferedreader ist, der duchgand wird mit readLine durchgeführt
sb.append(line + "\n"); //Hier wird der string line nach \n gesplittet (newline)
}
String result; //String result definieren
result = sb.toString(); //Hier wird der inhalt vom Stringbuilder zum String result hinzugefühgt
Log.d("DataHandler", "Append String " + result);
if(result != null){
try{
final String userid;
JSONObject holder = new JSONObject(result); //Result in ein JSONObject konvertieren
userid = holder.getString("id");
Log.d("JSON-Response", userid);
final Intent i = new Intent(LoginActivity.this, LoginActivity.class); --> THIS DOSN?T WORK!
i.putExtra("ID", userid);
}catch(Exception e){
e.printStackTrace();
Log.e("DataHandler", "Cannot read JSON"+e);
}
}
}
} catch(Exception e){
e.printStackTrace();
Log.e("DataHandler", "Cannot Estabilish Connection"+e);
}
}
};
t.start(); //Thread starten
}
Thx for your help in advance
Have a nice day!
replace
final Intent i = new Intent(LoginActivity.this, LoginActivity.class);
withi.putExtra("ID", userid);
add handle in class like below :
hope useful to you…