• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

android – 用户输入的URL编码

Android 来源:Muhammad Imanudin 8次浏览

我将用户输入编码到一个单一的URL,我使用Uri.encode但错误响应仍然是400.如何使编码工作?android – 用户输入的URL编码

的结果是,“空间”被替换为“%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; 
} 

任何帮助,将不胜感激。


===========解决方案如下:

修改代码,这样

@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); 
} 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)