2016-04-27 67 views
0

我的资产文件夹中有Json文件。我正在使用AutoCompleteTextView来过滤使用php作为Json的远程服务器的结果。但我想用我的Json文件而不是服务器响应Json。使用Json文件而不是服务器响应并将其用于过滤

这是我的代码。

private static final String url = AppConfig.URL_JSON_AVAILABLE_PRODUCTS; 

    @Override 
     public Filter getFilter() { 
      return new Filter() { 
       @Override 
       protected FilterResults performFiltering(CharSequence constraint) { 
        FilterResults filterResults = new FilterResults(); 
        if ((constraint == null || constraint.length() == 0)) { 
         filterResults.values = locations; 
         filterResults.count = locations.size(); 
        } else { 
         mResults = getParseJsonWCF(constraint.toString()); 
         locations.clear(); 
         for (int i = 0; i < mResults.size(); i++) { 
          if (mResults.get(i).getMatName().toUpperCase().startsWith(constraint.toString().toUpperCase())) { 
           //a = new_suggestions.get(i).getMatNo(); 
           locations.add(mResults.get(i)); 
          } 
         } 
         // Assign the data to the FilterResults 
         filterResults.values = locations; 
         filterResults.count = locations.size(); 
         //notifyDataSetChanged(); 
        } 
        return filterResults; 
       } 


       @SuppressWarnings("unchecked cast") 
       @Override 
       protected void publishResults(CharSequence constraint, FilterResults results) { 
        if (results != null && results.count > 0) { 
         mResults = (List<Product>) results.values; 
         //setArrayList(mResults); 
         notifyDataSetChanged(); 
        } else { 
         notifyDataSetInvalidated(); 
        } 
       } 
      }; 
     } 

     public List<Product> getParseJsonWCF(String sName) { 

      List<Product> ListData = new ArrayList<Product>(); 

      try { 
       String temp = sName.replace(" ", "%20"); 
       URL js = new URL(url + temp); 
       URLConnection jc = js.openConnection(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream())); 
       String line = reader.readLine(); 

       JSONObject jsonResponse = new JSONObject(line); 
       JSONArray jsonArray = jsonResponse.getJSONArray("feed"); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject r = jsonArray.getJSONObject(i); 
        ListData.add(new Product(r.getInt("matnum"), r.getString("matname"), r.getString("code"), r.getString("matgrp"), r.getString("mattype"))); 
       } 

      } catch (Exception e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      return ListData; 

     } 

我想用我的Json文件的数据来代替这里的url。我不知道如何实现这一点,把它的URL

回答

2

您可以保存您JsonResponse在一个文件作为字符串,当你从服务器获取这样

private void writeToFile(String data) { 
    try { 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("json.txt", Context.MODE_PRIVATE)); 
     outputStreamWriter.write(data); 
     outputStreamWriter.close(); 
    } 
    catch (IOException e) { 
     Log.e("Exception", "File write failed: " + e.toString()); 
    } 
} 

而且同第一时间响应你可以阅读文件内容,这样

private String readFromFile() { 

    String ret = ""; 

    try { 
     InputStream inputStream = openFileInput("json.txt"); 

     if (inputStream != null) { 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
      String receiveString = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 

      while ((receiveString = bufferedReader.readLine()) != null) { 
       stringBuilder.append(receiveString); 
      } 

      inputStream.close(); 
      ret = stringBuilder.toString(); 
     } 
    } 
    catch (FileNotFoundException e) { 
     Log.e("login activity", "File not found: " + e.toString()); 
    } catch (IOException e) { 
     Log.e("login activity", "Can not read file: " + e.toString()); 
    } 

    return ret; 
} 

字符串,并从阅读文件获取字符串可以解析这样的例如

JSONObject jsonObj = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

        // looping through All Contacts 
        for (int i = 0; i < contacts.length(); i++) { 
         JSONObject c = contacts.getJSONObject(i); 

         String id = c.getString(TAG_ID); 
         String name = c.getString(TAG_NAME); 
         String email = c.getString(TAG_EMAIL); 
         String address = c.getString(TAG_ADDRESS); 
         String gender = c.getString(TAG_GENDER); 
} 
相关问题