wAsync 抛出 ClassNotFondException
wAsync throws ClassNotFondException
我一直在尝试使用 wAsync 为 OpenHab 服务器 编写一个 android 客户端。
我正在使用带有 SDK 版本 21 的 android 工作室。
我的代码:
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.atmosphere.wasync.ClientFactory;
import org.atmosphere.wasync.Decoder;
import org.atmosphere.wasync.Encoder;
import org.atmosphere.wasync.Event;
import org.atmosphere.wasync.Function;
import org.atmosphere.wasync.Request;
import org.atmosphere.wasync.RequestBuilder;
import org.atmosphere.wasync.Socket;
import org.atmosphere.wasync.impl.AtmosphereClient;
import org.codehaus.jackson.map.ObjectMapper;
import android.os.Handler;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private Button act;
private TextView view;
private final String serverIp="http://demo.openhab.org:8080/rest/items/DemoSwitch/state";
private final static ObjectMapper mapper=new ObjectMapper();
private final Handler uiHandler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
act = (Button) findViewById(R.id.act);
view = (TextView) findViewById(R.id.view);
AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);
RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(serverIp)
.trackMessageLength(true)
.encoder(new Encoder<State, String>() {
@Override
public String encode(State data) {
try {
return mapper.writeValueAsString(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
})
.decoder(new Decoder<String, State>() {
@Override
public State decode(Event type, String data) {
data = data.trim();
// Padding
if (data.length() == 0) {
return null;
}
if (type.equals(Event.MESSAGE)) {
try {
return mapper.readValue(data, State.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
})
.transport(Request.TRANSPORT.LONG_POLLING);
final Socket socket = client.create();
try {
socket.on("message", new Function<State>() {
@Override
public void on(final State t) {
uiHandler.post(new Runnable() {
@Override
public void run() {
view.append("State " + t.getState());
}
});
}
}).on(new Function<Throwable>() {
@Override
public void on(Throwable t) {
view.setText("ERROR 3: " + t.getMessage());
t.printStackTrace();
}
}).open(request.build());
} catch (IOException e) {
e.printStackTrace();
}
act.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
socket.fire(new State("ON"));
Log.d("Client", "Client sent message");
} catch (Throwable e) {
view.setText("ERROR 3: " + e.getMessage());
e.printStackTrace();
}
}
});
}
}
我已经添加了互联网和访问网络状态的权限。
从 Maven Central 添加了依赖项 wasync-2.0.0-all。
但是连接不成功。我得到的错误是:
Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.security.util.HostnameChecker" on path: DexPathList[[zip file "/data/app/com.att_lnx_admin.atmosphereclient-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.att_lnx_admin.atmosphereclient-1, /system/lib]]
有人请帮我解决这个问题。
问题与 AHC 库有关。本周将完成新版本,因此您只需要将 async-http-client 更新到 1.9.9。
您的服务器端似乎不是最新的。尝试在 Android 端使用以前的版本:
编译 'org.atmosphere:wasync:1.4.3'
编辑:使用新发布的 1.9.10 版本解决。这是最后的工作 build.gradle:
compile ('com.ning:async-http-client:1.9.10')
compile ('org.atmosphere:wasync:2.0.0') {
exclude group: 'com.ning', module: 'async-http-client'
}
感谢 wasync 和 async-http-client 的创建者
https://github.com/AsyncHttpClient/async-http-client/tree/async-http-client-1.9.10
今天 async-hhtp-client 1.9.9 已经发布。我在我的 android build.gradle 文件中试过这个:
compile ('com.ning:async-http-client:1.9.9')
compile ('org.atmosphere:wasync:2.0.0') {
exclude group: 'com.ning', module: 'async-http-client'
}
和 sun.security.util.HostnameChecker NoClassDefFoundError 消失了,但是当我进行套接字连接时,我有另一个错误:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ning/http/client/providers/netty/NettyAsyncHttpProviderConfig;
at org.atmosphere.wasync.impl.ClientUtil.createDefaultAsyncHttpClient(ClientUtil.java:35)
at org.atmosphere.wasync.impl.ClientUtil.create(ClientUtil.java:74)
at org.atmosphere.wasync.impl.AtmosphereClient.create(AtmosphereClient.java:41)
我一直在尝试使用 wAsync 为 OpenHab 服务器 编写一个 android 客户端。
我正在使用带有 SDK 版本 21 的 android 工作室。
我的代码:
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.atmosphere.wasync.ClientFactory;
import org.atmosphere.wasync.Decoder;
import org.atmosphere.wasync.Encoder;
import org.atmosphere.wasync.Event;
import org.atmosphere.wasync.Function;
import org.atmosphere.wasync.Request;
import org.atmosphere.wasync.RequestBuilder;
import org.atmosphere.wasync.Socket;
import org.atmosphere.wasync.impl.AtmosphereClient;
import org.codehaus.jackson.map.ObjectMapper;
import android.os.Handler;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private Button act;
private TextView view;
private final String serverIp="http://demo.openhab.org:8080/rest/items/DemoSwitch/state";
private final static ObjectMapper mapper=new ObjectMapper();
private final Handler uiHandler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
act = (Button) findViewById(R.id.act);
view = (TextView) findViewById(R.id.view);
AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);
RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(serverIp)
.trackMessageLength(true)
.encoder(new Encoder<State, String>() {
@Override
public String encode(State data) {
try {
return mapper.writeValueAsString(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
})
.decoder(new Decoder<String, State>() {
@Override
public State decode(Event type, String data) {
data = data.trim();
// Padding
if (data.length() == 0) {
return null;
}
if (type.equals(Event.MESSAGE)) {
try {
return mapper.readValue(data, State.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
})
.transport(Request.TRANSPORT.LONG_POLLING);
final Socket socket = client.create();
try {
socket.on("message", new Function<State>() {
@Override
public void on(final State t) {
uiHandler.post(new Runnable() {
@Override
public void run() {
view.append("State " + t.getState());
}
});
}
}).on(new Function<Throwable>() {
@Override
public void on(Throwable t) {
view.setText("ERROR 3: " + t.getMessage());
t.printStackTrace();
}
}).open(request.build());
} catch (IOException e) {
e.printStackTrace();
}
act.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
socket.fire(new State("ON"));
Log.d("Client", "Client sent message");
} catch (Throwable e) {
view.setText("ERROR 3: " + e.getMessage());
e.printStackTrace();
}
}
});
}
}
我已经添加了互联网和访问网络状态的权限。
从 Maven Central 添加了依赖项 wasync-2.0.0-all。
但是连接不成功。我得到的错误是:
Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.security.util.HostnameChecker" on path: DexPathList[[zip file "/data/app/com.att_lnx_admin.atmosphereclient-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.att_lnx_admin.atmosphereclient-1, /system/lib]]
有人请帮我解决这个问题。
问题与 AHC 库有关。本周将完成新版本,因此您只需要将 async-http-client 更新到 1.9.9。
您的服务器端似乎不是最新的。尝试在 Android 端使用以前的版本: 编译 'org.atmosphere:wasync:1.4.3'
编辑:使用新发布的 1.9.10 版本解决。这是最后的工作 build.gradle:
compile ('com.ning:async-http-client:1.9.10')
compile ('org.atmosphere:wasync:2.0.0') {
exclude group: 'com.ning', module: 'async-http-client'
}
感谢 wasync 和 async-http-client 的创建者 https://github.com/AsyncHttpClient/async-http-client/tree/async-http-client-1.9.10
今天 async-hhtp-client 1.9.9 已经发布。我在我的 android build.gradle 文件中试过这个:
compile ('com.ning:async-http-client:1.9.9')
compile ('org.atmosphere:wasync:2.0.0') {
exclude group: 'com.ning', module: 'async-http-client'
}
和 sun.security.util.HostnameChecker NoClassDefFoundError 消失了,但是当我进行套接字连接时,我有另一个错误:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ning/http/client/providers/netty/NettyAsyncHttpProviderConfig;
at org.atmosphere.wasync.impl.ClientUtil.createDefaultAsyncHttpClient(ClientUtil.java:35)
at org.atmosphere.wasync.impl.ClientUtil.create(ClientUtil.java:74)
at org.atmosphere.wasync.impl.AtmosphereClient.create(AtmosphereClient.java:41)