我正在开发一个基于套接字的应用程序。我正在读取数据并转换为JSON,然后在List-view中显示。
套接字数据读取如下:
public String clientReceive(int check) {
try {
if (s != null) {
while (s.isConnected()) {
in = new BufferedReader(new InputStreamReader(
s.getInputStream()));
int nRead;
char[] data = new char[8192];
String decoded = "";
String symbs = "";
while ((nRead = in.read(data, 0, data.length)) != -1) {
decoded = new String(data, 0, nRead);
decoded = new String(decoded.getBytes(),"UTF-8");
return decoded;
}
}
} catch (SocketException e) {
}catch (Exception e) {
// TODO: handle exception
}
// return null;
return null;
}
Asyn Task doInBackgroung方法和postExecute方法是:
@Override
protected ArrayList<User> doInBackground(Void... params) {
//isCycleQuit=false;
for(int i=0;i<1000;i++){
if (isCancelled()) {
System.out.println("**** All timers killed ***");
break;
}
}
JSONmessage += client.clientReceive(1);
message=JSONmessage;
JSONmessage="";
//System.out.println("MESSAGE Socket::" + message);
return null;
}
protected void onPostExecute(ArrayList<User> result) {
if (!message.contains("null")) {
printJson();
// updateList();
displayView();
}
}
我正在使用显示视图方法显示数据。
public void displayView() {
try {
if (!marketWatch.equalsIgnoreCase("custom")) {
if (!swapBool)
adb = new RSSListAdaptor(DefaultMarketWatch.this,
R.layout.rssitemview, list);
else {
adb = new RSSListAdaptor(DefaultMarketWatch.this,
R.layout.arabiclist, list);
}
} else {
if (!swapBool)
adb = new RSSListAdaptor(DefaultMarketWatch.this,
R.layout.customlayout, list);
else {
adb = new RSSListAdaptor(DefaultMarketWatch.this,
R.layout.ar_customlayout, list);
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
我需要每秒读取数据并显示。我大部分时间都能看到列表视图。但有时候数据显示在控制台上。但不是更新UI并保持领先。
我错了。请帮助。
感谢。