如何将字符串从Java服务器类传递到Android活动?

如何将字符串从Java服务器类传递到Android活动?,java,android,android-intent,android-emulator,android-activity,Java,Android,Android Intent,Android Emulator,Android Activity,所以基本上,我有一个Android手机,它有一个应用程序作为客户端,向Java服务器发送字符串,然后服务器依次将字符串传递给Android活动,该活动在Android模拟器中运行。仿真器将用于显示字符串 现在,我可以将字符串从手机应用发送到服务器,但当服务器试图将字符串传递给emulator正在运行的活动时,会发生以下错误: Exception in thread "main" java.lang.NoClassDefFoundError: android/app/Application at

所以基本上,我有一个Android手机,它有一个应用程序作为客户端,向Java服务器发送字符串,然后服务器依次将字符串传递给Android活动,该活动在Android模拟器中运行。仿真器将用于显示字符串

现在,我可以将字符串从手机应用发送到服务器,但当服务器试图将字符串传递给emulator正在运行的活动时,会发生以下错误:

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
以下是我的Java服务器代码:

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();
                }
            }
        }
    }
}
}
错误发生在上面代码的上下文意图部分,我试图通过意图将字符串发送到Android活动

我使用下面的代码获取应用程序的上下文,但我不确定它是否有效:

public class ApplicationContext extends Application{

private static Context context;

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

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

有没有办法在普通Java类中调用或使用Android类?或者有没有其他方法可以将字符串从Java类传递到Android活动类?

如果独立Java应用程序位于仿真器之外,则无法使用Android API。我建议您使用服务器完成这两项任务。一个客户端将数据放在它(您的手机)上,另一个客户端(仿真器)从服务器读取数据


您不应该忘记,从理论上讲,仿真器是仿真器“容器”中的另一个设备。因此,据我所知,模拟器将有一个NAT IP地址,您的PC将是您的网关

服务器被另一个应用程序调用,我把它安装在我的android手机上。第一个是正确的,其中服务器是一个独立的Java项目应用程序,必须与Android Emulator对话。我尝试将Java应用程序与Emulator中运行的应用程序放在同一个项目中,但它仍然产生相同的错误。。。这是为什么?问题是在哪里运行它,而不是在哪里编译它。如果您在emulator之外运行独立应用程序,您将无法访问Android上下文对象,因为它是在运行时生成的。