0
私の資産フォルダにJsonファイルがあります。私はAutoCompleteTextViewを使用して、JsonとしてPHPを使用してリモートサーバから来た結果をフィルタリングしています。しかし、私は、サーバー応答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;
}
URLの代わりに私のJsonファイルのデータを使用したいと思います。私はあなたがこの
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());
}
}
と同じように、サーバからの最初の時間応答を取得するときは、文字列としてファイルにあなたのJsonResponseを保存することができますように実装する方法を知っていると、URLの中に