从localhost服务器

时间:2017-03-24 06:30:28

标签: android json android-asynctask

  • 当我从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;
            }
        }
    
    
       }
    

1 个答案:

答案 0 :(得分:0)

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