私はsocket-ioを使用してandroidアプリを作成しています。すべてのデータはsocket-ioを介してサーバーからandroidアプリに取得され、このデータはリストビューでフォーマットする必要があります。ただし、arrayadapter でのデータのロードには問題があります。私の MainActivity.class は
public class MainActivity extends Activity {
ListView list;
String[] namesArr ;
ArrayList<String> mylist = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new databasehelper(this);
list = (ListView)findViewById(R.id.newslist);
SocketIO socket = null;
try {
socket = new SocketIO("http://**************/");
//socket = new SocketIO("http://localhost:7010/");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
// Log.d("json",""+json);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
// Log.d("json",""+ data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
@Override
public void onMessage(JSONArray args,IOAcknowledge remoteAcknowledge) {
// TODO Auto-generated method stub
Log.d("main", ""+args);
try {
JSONArray object = args.getJSONArray(0);
Log.d("object",""+object);
for(int n = 0; n < object.length(); n++)
{
String ob2 = object.getString(n);
Log.d("ob2",""+ob2);
//String[] myarray = new String[n];
mylist.add(ob2);
}
Log.d("arraylist",""+mylist);
namesArr = new String[mylist.size()];
for (int i = 0; i < mylist.size(); i++) {
namesArr[i] = mylist.get(i);
Log.d("string array element",namesArr[i]);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
display(mylist);
}
private void display(ArrayList<String> mylist) {
// TODO Auto-generated method stub
ArrayAdapter<String> aa = new ArrayAdapter<String> getApplicationContext(),android.R.layout.simple_list_item_1,mylist);
Log.d("real Arraylist",""+mylist);
//Log.d("array adapter",aa.toString());
// list.setAdapter(aa);
}
}
そして、私はこのようなエラーを受け取ります
11-08 07:09:24.783: W/System.err(5775): at com.mayur.socketio.IOConnection.transportMessage(IOConnection.java:703)
11-08 07:09:24.792: W/System.err(5775): at com.mayur.socketio.WebsocketTransport.onMessage(WebsocketTransport.java:82)
11-08 07:09:24.814: W/System.err(5775): at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:361)
11-08 07:09:24.814: W/System.err(5775): at org.java_websocket.WebSocketImpl.deliverMessage(WebSocketImpl.java:565)
11-08 07:09:24.823: W/System.err(5775): at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:331)
11-08 07:09:24.823: W/System.err(5775): at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:152)
11-08 07:09:24.823: W/System.err(5775): at org.java_websocket.client.WebSocketClient.interruptableRun(WebSocketClient.java:247)
11-08 07:09:24.823: W/System.err(5775): at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:193)
11-08 07:09:24.823: W/System.err(5775): at java.lang.Thread.run(Thread.java:841)
11-08 07:09:24.832: W/System.err(5775): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-08 07:09:24.882: W/System.err(5775): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
11-08 07:09:24.882: W/System.err(5775): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
11-08 07:09:24.893: W/System.err(5775): at android.view.View.requestLayout(View.java:15792)
11-08 07:09:24.893: W/System.err(5775): at android.view.View.requestLayout(View.java:15792)
11-08 07:09:24.893: W/System.err(5775): at android.view.View.requestLayout(View.java:15792)
11-08 07:09:24.904: W/System.err(5775): at android.view.View.requestLayout(View.java:15792)
11-08 07:09:24.904: W/System.err(5775): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
11-08 07:09:24.913: W/System.err(5775): at android.view.View.requestLayout(View.java:15792)
11-08 07:09:24.913: W/System.err(5775): at android.widget.AbsListView.requestLayout(AbsListView.java:1837)
11-08 07:09:24.933: W/System.err(5775): at android.widget.ListView.setAdapter(ListView.java:491)
11-08 07:09:24.933: W/System.err(5775): at com.mayur.socketio.MainActivity$1.onMessage(MainActivity.java:147)
11-08 07:09:24.943: W/System.err(5775): at com.mayur.socketio.IOConnection.onMessage(IOConnection.java:937)
11-08 07:09:24.943: W/System.err(5775): at com.mayur.socketio.IOConnection.transportMessage(IOConnection.java:698)
私のコードの問題と解決策を教えてください??? 前もって感謝します