0

I'm trying to load webservice in to android webview.my request url contain id.what i want to do is take "j" value in for loop and run webpage according to "j" value.when first webpage load,i want to load second web page after 5 seconds.this is my code

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) {

                }



                }
        }
    };
Yasi
  • 23
  • 6
  • Asked a bazillion time... https://stackoverflow.com/q/1921514 or use the "Search" box... – shkschneider Jun 17 '15 at 09:03
  • possible duplicate of [How to run a Runnable thread in Android?](http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android) – shkschneider Jun 17 '15 at 09:03

1 Answers1

0

Use Timer

Try following code

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) {

                }


            }
    }
};
Rohit
  • 2,646
  • 6
  • 27
  • 52