简体   繁体   中英

How to pass a String from a Java server class to an Android Activity?

So basically, I have an Android phone with an app that acts as the client and sends a String to a Java server and then the Server in turn passes the String to an Android Activity that is running in an Android Emulator . The emulator will be used to display the the String.

Now, I was able to send the String from the phone app to the server , but when the server is trying to pass the String to the activity that the emulator is running, this error occurs:

Exception in thread "main" java.lang.NoClassDefFoundError: android/app/Application
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at cs178.homework.locationupdate.Server.main(Server.java:55)
Caused by: java.lang.ClassNotFoundException: android.app.Application
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more

Here is my Java Server code:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.content.Context;
import android.content.Intent;
import cs178.homework.locationupdate.ApplicationContext;
import cs178.homework.locationupdate.MainActivity;

public class Server {

public static void main(String[] args){

    ServerSocket serverSocket = null;
    Socket clientSocket = null;

    //create a reader for our socket object data
    DataInputStream dataInputStream = null;
    //create a writer for our socket object data
    DataOutputStream dataOutputStream = null;

    int portNo = 8888;

    //attempt to create a new server for data transfers
    try {
        serverSocket = new ServerSocket(portNo);
        System.out.println("Server created successfully. Currently listening to port " + portNo);
    } catch (IOException e) {
        System.out.println("Failed to create server...");
        e.printStackTrace();
    }

    //process continues if the server has been established then generate a server-client connection
    while(true){
        try {
            clientSocket = serverSocket.accept();
            System.out.println("IP: " + clientSocket.getInetAddress());

            //get the sent data of the client's socket
            dataInputStream = new DataInputStream(clientSocket.getInputStream());
            String clientMsg = dataInputStream.readUTF();
            System.out.println("Message: " + clientMsg);

            //Here is where the error occured...
            Context context = ApplicationContext.getContext();
            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("sms_location", clientMsg);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);

            dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
            dataOutputStream.writeUTF("Message Received!");

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if( clientSocket!= null){
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if( dataInputStream!= null){
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if( dataOutputStream!= null){
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
}

The error occurred in the Context and Intent part of the code above where I'm trying to send the String to an Android Activity through Intent.

I used the code below to get the Context of the application but I'm not sure if it works:

public class ApplicationContext extends Application{

private static Context context;

public void onCreate() {
    context = getApplicationContext();
}

public static Context getContext() {
    return context;
}
}

Is there any way to call or use Android classes in a plain Java class? Or is there any other way to pass a String from a Java class to an Android Activity class?

If the standalone Java app is outside the emulator, you cannot use the Android API. I would suggest you to use your server for both tasks. One client puts data on it (your phone), and the other client (emulator) reads data from the server.

You shouldn't forget that the emulator is in theory another device inside the emulator "container". So, as far as I know, the emulator will have a NAT IP-Address and your PC will be your gateway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM