我正在尝试将webservice加载到android webview.my请求url包含id。我想要做的是在for循环中取“j”值并根据“j”值运行网页。当第一个网页加载时,我想在5秒后加载第二个网页。这是我的代码
final Runnable myRunnable = new Runnable() {
public void run() {
if(i%10==0){
try {
for (int j = 1; j <=3; j++) {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String Date = df.format(c.getTime());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JSONObject json = new JSONObject();
json.put("id", j);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
String url = "http://myweb/index?id="+""+j+"";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
// Log.i("Read from server", result);
JSONObject jsonObj = new JSONObject(result);
String value = jsonObj.getString("posts");
slideWebView.loadUrl(value);// webview process
WebSettings webSettings = slideWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// myWebView.setInitialScale(100);
slideWebView.getSettings().setLoadWithOverviewMode(true);
slideWebView.getSettings().setUseWideViewPort(true);
}
//
}
} catch (Throwable t5) {
}
}
}
};
答案 0 :(得分:0)
使用Timer
尝试以下代码
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
pollWebService();
}
}, 0, 5000);
public void pollWebService()
{
final Runnable myRunnable = new Runnable() {
public void run() {
if(i%10==0){
try {
for (int j = 1; j <=3; j++) {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String Date = df.format(c.getTime());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JSONObject json = new JSONObject();
json.put("id", j);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
String url = "http://myweb/index?id="+""+j+"";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
// Log.i("Read from server", result);
JSONObject jsonObj = new JSONObject(result);
String value = jsonObj.getString("posts");
slideWebView.loadUrl(value);// webview process
WebSettings webSettings = slideWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// myWebView.setInitialScale(100);
slideWebView.getSettings().setLoadWithOverviewMode(true);
slideWebView.getSettings().setUseWideViewPort(true);
}
//
}
} catch (Throwable t5) {
}
}
}
};