我在应用程序中使用Volley库。我使用一种方法通过意图将自己从活动 A 转移到活动 B。
StringURL StringURL
-------------- intent --------------
| Activity A | >---->> | Activity B |
-------------- --------------
mystring url ====> http://www.articler.ir/android_php/search.php?Method=rate&CAT=مهندسی%20عمران
然后在活动 B 中,我使用服务器从服务器接收内容,但显然,这些内容在 Android 上接收不到 5 个空。请准确地指导我在哪里。我再次将这个问题放在此链接中 列表视图没有显示任何棒棒糖版本并找到根本问题,但我不明白为什么Android 4.4中服务器的响应为空。但在Android 5+上也是如此。如何解决问题?请帮助我,再次感谢你。
安卓 5-
安卓 5+
在活动 B 中:
public class FragmentOne extends Fragment {
private List<Model> myList = new ArrayList<Model>();
private ArrayList<String> authors;
private ArrayList<String> keywords;
String FirstURL ;
String SearchURL;
//my stirng url ===>
// http://www.articler.ir/android_php/search.php?Method=rate&CAT=مهندسی%20عمران
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
final SwipeRefreshLayout swipeRefreshLayout = rootView.findViewById(R.id.fm_one);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), myList);
final ListView listView1 = (ListView) rootView.findViewById(R.id.myList1);
Intent intent = getActivity().getIntent();
FirstURL = intent.getStringExtra("SearchURL");
SearchURL = FirstURL.replaceAll(" ", "%20");
Log.d("Search", "Search: " + SearchURL);
myList.removeAll(myList);
swipeRefreshLayout.setRefreshing(true);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(SearchURL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("VolleyRes", "response: " + response);
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
Model model = new Model();
model.setTitle(jsonObject.getString("title"));
model.setMainText(jsonObject.getString("mainText"));
model.setrate(jsonObject.getDouble("rate"));
model.setYear(jsonObject.getInt("year"));
model.setConfName(jsonObject.getString("confName"));
model.setProductId(jsonObject.getInt("productId"));
model.setCountView(jsonObject.getInt("countView"));
model.setPdfURL(jsonObject.getString("pdfURL"));
// authors is json array
authors = new ArrayList<String>();
JSONArray authorArray = jsonObject.getJSONArray("authors");
for (int j = 0; j < authorArray.length(); j++) {
if (!authors.contains(authorArray.getString(j)) && !authorArray.isNull(j)) {
authors.add((String) authorArray.get(j));
}
}
model.setAuthor(authors);
// Keywords is json array
keywords = new ArrayList<String>();
JSONArray keywordArray = jsonObject.getJSONArray("Keywords");
for (int k = 0; k < keywordArray.length(); k++) {
if (!keywords.contains(keywordArray.getString(k)) && !keywordArray.isNull(k)) {
keywords.add((String) keywordArray.get(k));
}
}
model.setKeywords(keywords);
myList.add(model);
} catch (JSONException e) {
e.printStackTrace();
}
}
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("VolleyErr", "ErrorMassage: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
swipeRefreshLayout.setRefreshing(false);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
swipeRefreshLayout.setRefreshing(true);
int year = myList.get(position).getYear();
int productId = myList.get(position).getProductId();
int countView = myList.get(position).getCountView();
double rate = myList.get(position).getrate();
String title = myList.get(position).getTitle();
String mainText = myList.get(position).getMainText();
String confName = myList.get(position).getConfName();
ArrayList authors = myList.get(position).getAuthor();
ArrayList keywords = myList.get(position).getKeywords();
String pdfURL = myList.get(position).getPdfURL();
SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("productId", productId); //InputString: from the EditText
editor.putInt("countView", countView);
editor.putFloat("rate", (float) rate);
editor.putString("pdfURL",pdfURL);
editor.putString("title",title);
editor.putString("mainText",mainText);
editor.putString("confName",confName);
editor.putString("authors", String.valueOf(authors));
editor.putString("keywords", String.valueOf(keywords));
editor.putInt("year", year);
editor.commit();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
Intent intent = new Intent(getContext(),DetailActivity.class);
startActivity(intent);
// finish();
}
}, 1500);
}
});
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh() {
((MyTabActivity)getActivity()).refreshNow();
Toast.makeText(getContext(), "بروزرسانی صفحه!", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
我的应用控制器:
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
我意识到不支持 Android 上波斯语字体小于 5 的 URL。所以我需要通过编码来更改字符串,所以我使用下面的方法来更改它。
//this is initial string url ====> http://www.articler.ir/android_php/search.php?Method=rate&CAT=مهندسی%20عمران
String MyKey = مهندسی عمران;
final String EncodeKey= URLEncoder.encode(MyKey , "UTF-8");
String EncodeURL = " http://www.articler.ir/android_php/search.php?Method=rate&CAT= " + EncodeKey;
更改此部分后,我的响应是正确的,例如5+版本;