如何在指定的时间间隔后在textview中显示来自jsons reposne的文本?

时间:2016-10-22 12:06:29

标签: android

我有一个textview,其中我想显示我在指定的时间间隔内从服务器响应中获得的文本,以及当数据集结束时我想再次从textview中开始查看。我怎么能这样做

代码: -

private CDealAppDatastorage item;
private String TAG = MainActivity.class.getSimpleName();
private ArrayList<CDealAppDatastorage> s_oDataset;


try{
        String json;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dealcategory","DEALTEST");
        json = jsonObject.toString();
        Log.e(TAG,"Server Reqeust::-"+json);

        final String m_szTicker = "http://14.192.16.1555:8080/rest/json/metallica/getDealListsJSON?";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_szTicker,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.e(TAG,"Server Response::"+response);
                try{

                    if (response.getString("resultdescription").equalsIgnoreCase("Transaction Successful")){
                        JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from r
                        for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
                            JSONObject post = posts.getJSONObject(i);// counting deal based on index
                            item = new CDealAppDatastorage();// creating object of DealAppdata storage
                            item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
                            s_oDataset.add(item);// add all items in ArrayList

                        }
                        int i;
                        for (i=0;i<s_oDataset.size();i++){
                            Log.e(TAG,"Data Size:"+i);

                            final int finalI = i;
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    CDealAppDatastorage m = s_oDataset.get(finalI);
                                    m_Text.setText(m.getM_szHeaderText());
                                }
                            },6000);

                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());// creating object of Request queue
        requestQueue.add(jsonObjectRequest);// add json Object request to request queue
    }catch (Exception e){
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

在处理程序内的textview中设置文本时替换此代码,如果要在处理程序中更改文本,则必须使用runonuiThread进行ui更新

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.frame), CGRectGetMaxY(self.view.frame))];
[webView setDelegate:self];
[self.view addSubview:webView];

NSString *sessionId = [[OCRESTAPIClient sharedClient] sessionId];
NSString *value = [@"xid=" stringByAppendingString:sessionId];

NSURL *url = [NSURL URLWithString:@"http://MySite.ru/cart"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:value forHTTPHeaderField:@"Cookie"];

[webView loadRequest:request];

答案 1 :(得分:0)

使用此代码..可以在5秒后在textview中显示值

  Handler handler = new Handler(); 
  handler.postDelayed(new Runnable() {
      @Override 
      public void run() { 
           txtView.setText(your_text); 
      } 
   }, 5000);