android - 用户输入

时间:2017-12-18 04:16:12

标签: android string parsing android-volley urlencode

我将用户输入编码为单个URL,我使用Uri.encode但错误响应仍为400.如何使编码工作?

结果是"空间"替换为"%20"但响应仍然失败

这是我的代码。

public void OnQuerySubmit() {
    searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            String UrlEncode = AppConfig.URL_GETJOBS + Uri.encode(query);
            progressDialog.setMessage("Mencari...");
            showDialog();
            jobList.clear();
            getJobs(UrlEncode);
            homeRecyclerViewAdapter.notifyDataSetChanged();
            hideDialog();
            return false;
        }
    });
}

public List<Job> getJobs(String urlGetjobs) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlGetjobs, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "onResponse: " + response.toString());
            try {

                boolean error = response.getBoolean("error");

                if(!error) {
                    JSONArray  transaksiArray = response.getJSONArray("transaksi");
                    for(int i = 0; i < transaksiArray.length(); i++) {
                JSONObject seluruhTrans = transaksiArray.getJSONObject(i);

                Job job = new Job();
                job.setTitle(seluruhTrans.getString("title"));
                job.setNamaPerusahaan(seluruhTrans.getString("nama_perusahaan"));
                job.setLokasi(seluruhTrans.getString("lokasi"));
                job.setKriteria_1(seluruhTrans.getString("kriteria_1"));
                job.setKriteria_2(seluruhTrans.getString("kriteria_2"));
                job.setKriteria_3(seluruhTrans.getString("kriteria_3"));
                job.setGaji(seluruhTrans.getString("gaji"));
                job.setImg_logo(seluruhTrans.getString("img_logo"));
                job.setTanggal(seluruhTrans.getString("tanggal"));
                job.setId_post(seluruhTrans.getString("id_post"));

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("title", job.getTitle());
                editor.putString("nama_perusahaan", job.getNamaPerusahaan());
                editor.putString("lokasi", job.getLokasi());
                editor.putString("kriteria_1", job.getKriteria_1());
                editor.putString("kriteria_2", job.getKriteria_2());
                editor.putString("kriteria_3", job.getKriteria_3());
                editor.putString("gaji", job.getGaji());
                editor.putString("img_logo", job.getImg_logo());
                editor.putString("tanggal", job.getTanggal());
                editor.putString("id_post", job.getId_post());
                editor.commit();

                jobList.add(job);
                        }
                }
                homeRecyclerViewAdapter.notifyDataSetChanged();

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        if (error instanceof NoConnectionError || error instanceof TimeoutError || error instanceof NoConnectionError){
            Toast.makeText(getActivity(), "Please Check your Connection!", Toast.LENGTH_SHORT).show();
        }
    }
    });
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);
    return jobList;
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

像这样修改你的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_home, container, false);

    queue = Volley.newRequestQueue(getActivity());

    searchView = view.findViewById(R.id.search_view);
    recyclerView = view.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    jobList = new ArrayList<>();

    homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getActivity(), jobList);
    recyclerView.setAdapter(homeRecyclerViewAdapter);
    getJobs(""); //get your data as it is

    OnQuerySubmit();

    return view;

}

public void OnQuerySubmit() {
    searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            getJobs(query);
            return false;
        }
    });
}

public void getJobs(final String query) {
    StringRequest jsonObjectRequest = new StringRequest(Request.Method.GET, AppConfig.URL_GETJOBS, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "onResponse: " + response);
            try {
                JSONObject jsonObject=new JSONObject(response);
                boolean error = jsonObject.getBoolean("error");
                if (!error) {
                    jobList.clear();
                    JSONArray transaksiArray = jsonObject.JSONArray("transaksi");
                        ....
                        ....
                        ....
                    jobList.add(job);
                    adapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {

            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(Tag, error.toString());
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            if(!TextUtils.isEmpty(query){
                map.put("title", query);
            }else{}
            return map;
        }
    };
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}

答案 1 :(得分:0)

<section>
  <div class="content color">
    <p>
      Image
    </p>
  </div>

  <div class="content color">
    <p>Text</p>
  </div>

</section>
<section class="reverse">
  <div class="content color">
    <p>Image</p>
  </div>

  <div class="content color">
    <p>Text</p>
  </div>
</section>