【问题标题】:Rendering text from the localhost server从 localhost 服务器渲染文本
【发布时间】:2017-03-24 06:30:27
【问题描述】:
  • 当我从 localhost 获取文本时,有时它会显示在 textview 中,有时则不显示。
  • 我不知道该怎么做,但我检查了我所有的本地主机服务器,数据库在邮递员中表它工作正常。
  • 但是,当我与 android 集成时,有时文本不会显示

    public class BackGroundTask extends AsyncTask<RequestPackage, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    
    
    }
    
    @Override
    protected String doInBackground(RequestPackage... params) {
        return RequestHttpURLConnection.getData(params[0]);
    }
    
    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }
    
    @Override
    protected void onPostExecute(String result) {
        JSONObject jsonObject = null;
    
    
            switch (gridItemId) {
                case 0:
                    break;
                case 1: {
                    Chairmen_textView.setText(result);
                }
                break;
                case 2: {
                    Principal_textView.setText(result);
                   }  break;
    
                case 3:   try  {
    
    
                    JSONArray jsonArray = new JSONArray(result);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String trust_title = jsonObject1.getString("title");
                        String trust_content = jsonObject1.getString("trust_content");
                        String college_details = jsonObject1.getString("college_details");
                        Trust_title_textView.setText(trust_title);
                        Trust_content_textView.setText(trust_content);
                        College_details_textView.setText(college_details);
                    }
                   } catch (JSONException e) {
                    e.printStackTrace();
                } break;
    
    
                case 4: try {
    
                    JSONArray jsonArray = new JSONArray(result);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String management = jsonObject1.getString("title");
                        String headline = jsonObject1.getString("headline");
                        String powers = jsonObject1.getString("powers");
                        String name = jsonObject1.getString("name");
                        String member_title = jsonObject1.getString("member_title");
                        String member_content = jsonObject1.getString("member_content");
                        String end_line = jsonObject1.getString("end_line");
    
                        Management_textView.setText(management);
                        Headline_textView.setText(headline);
                        Power_textView.setText(powers);
                        Name_textView.setText(name);
                        Member_title_textView.setText(member_title);
                        Member_content_textView.setText(member_content);
                        End_line_textView.setText(end_line);
                    }
                  } catch (JSONException e) {
                    e.printStackTrace();
                }   break;
    
                case 5: try {
    
                    JSONArray jsonArray = new JSONArray(result);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String quality_title = jsonObject1.getString("title");
                        String subtitle_vision = jsonObject1.getString("subtitle_vision");
                        String subtitle_vision_content = jsonObject1.getString("vision_content");
                        String subtitle_mission = jsonObject1.getString("subtitle_mission");
                        String subtitle_mission_content = jsonObject1.getString("mission_content");
                        String subtitle_quality_policy = jsonObject1.getString("subtitle_quality_policy");
                        String subtitle_quality_policy_content = jsonObject1.getString("quality_policy_content");
    
    
                        Quality_Title_textView.setText(quality_title);
                        Subtitle_vision_textView.setText(subtitle_vision);
                        Subtitle_vision_content_textView.setText(subtitle_vision_content);
                        Subtitle_mission_textView.setText(subtitle_mission);
                        Subtitle_mission_content_textView.setText(subtitle_mission_content);
                        Subtitle_quality_policy_textView.setText(subtitle_quality_policy);
                        Subtitle_quality_policy_content_textView.setText(subtitle_quality_policy_content);
    
    
                    }
    
    
              } catch (JSONException e) {
                    e.printStackTrace();
                }      break;
                case 6:try {
    
                    JSONArray jsonArray = new JSONArray(result);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String location_title = jsonObject1.getString("title");
                        String location_content = jsonObject1.getString("location_content");
                        Location_title.setText(location_title);
                        Location_content.setText(location_content);
    
    
                    }
    
                     }catch (JSONException e) {
                    e.printStackTrace();
                } break;
            }
        }
    
    
       }
    

【问题讨论】:

  • logcat 中有任何异常的堆栈跟踪吗?
  • 不,没什么……这是在 onPostExecute 中编码的正确方法吗?
  • case 0: break;。在这种情况下不会显示太多你不觉得吗?
  • 您没有说明您的代码是如何流动的。选择哪种情况。结果的值。

标签: android json android-asynctask


【解决方案1】:

首先,我将 try catch 块包裹在开关周围以避免代码重复。其次,您只捕获 jsonexception,尝试用更一般的异常替换此异常。也许这有帮助

【讨论】:

  • 我已经有一段时间没有处理这个问题了,但是你有两个线程,后台线程和渲染 UI 的主线程。由于并发性,Textview 有时可能会在 UI 线程中更新(这是您看到文本时),有时是从后台线程的上下文中更新(这是您看不到文本更新时)。有一个名为“runOnUIThread”的方法,它接受一个可运行对象作为参数。尝试在这个 runnable/UIThread 中更新你的 textview 元素。让我知道这是否有帮助。
  • 非常糟糕的主意。该代码应该只显示在 onPostExecute 中。
  • @greenapps 如果我没听错,你的意思是在 UI 线程上渲染文本是个坏主意?你能说得更准确点吗?
猜你喜欢
  • 2017-02-13
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2016-05-22
相关资源
最近更新 更多