Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7043003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:14:04+00:00 2026-05-28T02:14:04+00:00

What I try to do Hello Guys, I’m trying to create a Login-Screen which

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:14:05+00:00Added an answer on May 28, 2026 at 2:14 am

    replace final Intent i = new Intent(LoginActivity.this, LoginActivity.class);
    i.putExtra("ID", userid);
    with

    Message progressMsg = new Message();
                            progressMsg.setData(your bundle);
                            handler.sendMessage();
    

    add handle in class like below :

    Handler handler = new Handler() {
    
        @Override
        public void handleMessage(android.os.Message msg) {
            Bundle bundle = msg.getData();
            Intent i = new Intent(LoginActivity.this, newActivity.class); 
            i.putExtra("ID", userid);
        }
    };
    

    hope useful to you…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I try to do Hello Guys, I try to create a login for
What I'm trying to do Hello Guys, I'm trying to create a SplashScreen which
Hello guys I was wondering why some websites which uses php (or any other
Hello guys this method sending file on client machine private void StartServer() { TcpListener
First off - hello, this is my first Stack Overflow question so I'll try
When I try this code: dict_a = dict_b = dict_c = {} dict_c['hello'] =
I have a C++ DLL with code like this: LogMessage( Hello world ); try
Hello I'm creating an os x application for which I try to add a
I try this in PHPMyadmin: Update wp_1_posts SET post_content='<strong>Hello </strong> <a href=http://stackoverflow.com>stackoverflow</a> you think
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.