したがって、基本的には、クライアントとして機能し、文字列をJava サーバーに送信するアプリを備えたAndroid 携帯電話があり、次にサーバーがその文字列をAndroid Emulatorで実行されているAndroid アクティビティに渡します。エミュレーターは文字列を表示するために使用されます。
これで、電話アプリからサーバーに文字列を送信できましたが、サーバーがエミュレーターが実行しているアクティビティに文字列を渡そうとすると、次のエラーが発生します。
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();
}
}
}
}
}
}
Intentを介して 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 Activity クラスに文字列を渡す他の方法はありますか?