gpt4 book ai didi

java - 如何将字符串从 Java 服务器类传递到 Android Activity ?

转载 作者:行者123 更新时间:2023-12-01 14:29:53 25 4
gpt4 key购买 nike

基本上,我有一个 Android 手机,其中有一个应用程序充当客户端,并将字符串发送到 Java 服务器,然后服务器依次传递该字符串到在 Android 模拟器 中运行的 Android Activity。模拟器将用于显示字符串。

现在,我能够将字符串从手机应用程序发送到服务器,但是当服务器尝试将字符串传递到模拟器正在运行的 Activity 时,会发生此错误:

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();
}
}
}
}
}
}

错误发生在上面代码的ContextIntent部分,我试图通过Intent将字符串发送到Android Activity。

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

public class ApplicationContext extends Application{

private static Context context;

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

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

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

最佳答案

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

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

关于java - 如何将字符串从 Java 服务器类传递到 Android Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16934094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com