android - 用户输入的URL编码

我将用户输入编码到一个单一的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);

}

回答:

package com.mimdudin.carekkerje.Activities; 

import android.app.ProgressDialog;

import android.content.Context;

import android.content.SharedPreferences;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Toast;

import com.android.volley.NoConnectionError;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.TimeoutError;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.StringRequest;

import com.android.volley.toolbox.Volley;

import com.miguelcatalan.materialsearchview.MaterialSearchView;

import com.mimdudin.carekkerje.Adapter.HomeRecyclerViewAdapter;

import com.mimdudin.carekkerje.Helper.AppController;

import com.mimdudin.carekkerje.Model.Job;

import com.mimdudin.carekkerje.R;

import com.mimdudin.carekkerje.Util.AppConfig;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.net.URLEncoder;

import java.util.ArrayList;

import java.util.List;

import static com.android.volley.VolleyLog.TAG;

/**

* A simple {@link Fragment} subclass.

*/

public class HomeFragment extends Fragment {

private RecyclerView recyclerView;

private HomeRecyclerViewAdapter homeRecyclerViewAdapter;

private List<Job> jobList;

private RequestQueue queue;

private MaterialSearchView searchView;

private ProgressDialog progressDialog;

private SharedPreferences sharedPreferences;

public HomeFragment() {

// Required empty public constructor

}

public static HomeFragment newInstance() {

HomeFragment homeFragment = new HomeFragment();

return homeFragment;

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setHasOptionsMenu(true);

}

@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.setHasFixedSize(true);

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

jobList = new ArrayList<>();

jobList = getJobs(AppConfig.URL_GETJOBS);

homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getActivity(), jobList);

recyclerView.setAdapter(homeRecyclerViewAdapter);

homeRecyclerViewAdapter.notifyDataSetChanged();

sharedPreferences = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);

progressDialog = new ProgressDialog(getContext());

progressDialog.setCancelable(false);

OnQuerySubmit();

return view;

}

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;

}

@Override

public boolean onQueryTextChange(String newText) {

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 void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

inflater.inflate(R.menu.menu_main, menu);

MenuItem item = menu.findItem(R.id.action_search);

searchView.setMenuItem(item);

super.onCreateOptionsMenu(menu, inflater);

}

public void showDialog() {

if (!progressDialog.isShowing()) {

progressDialog.show();

}

}

public void hideDialog(){

if (progressDialog.isShowing()) {

progressDialog.dismiss();

}

}

}

HomeRecyclerviewAdapter.java

package com.mimdudin.carekkerje.Adapter;

import android.app.FragmentManager;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.bumptech.glide.request.RequestOptions;

import com.mimdudin.carekkerje.Activities.HomeFragment;

import com.mimdudin.carekkerje.Activities.HomeFragmentDetail;

import com.mimdudin.carekkerje.Model.Job;

import com.mimdudin.carekkerje.R;

import java.util.List;

/**

* Created by master on 12/12/17.

*/

public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {

private Context context;

private List<Job> jobList;

public HomeRecyclerViewAdapter(Context context, List<Job> jobs){

this.context = context;

jobList = jobs;

}

@Override

public HomeRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext())

.inflate(R.layout.home_row, parent, false);

return new ViewHolder(view, context);

}

@Override

public void onBindViewHolder(HomeRecyclerViewAdapter.ViewHolder holder, int position) {

Job job = jobList.get(position);

holder.title.setText(job.getTitle());

holder.namaPerusahaan.setText(job.getNamaPerusahaan());

holder.kriteria_1.setText(job.getKriteria_1());

holder.kriteria_2.setText(job.getKriteria_2());

holder.kriteria_3.setText(job.getKriteria_3());

holder.gaji.setText(job.getGaji());

holder.lokasi.setText(job.getLokasi());

holder.tanggal.setText(job.getTanggal());

String img_logoLink = job.getImg_logo();

Glide.with(context)

.load(img_logoLink) // URL

.apply(new RequestOptions().placeholder(android.R.drawable.ic_dialog_info)

.error(android.R.drawable.ic_dialog_alert).centerCrop().dontAnimate()

// .centerCrop(), .crossFade(), .thumbnail(), .dontAnimate(), .dontTransform() BitmapTransformation(.circleCrop())

)

.into(holder.img_logo); //TARGET GAMBAR YANG NAK DIUBAH

}

@Override

public int getItemCount() {

return jobList.size();

}

public class ViewHolder extends RecyclerView.ViewHolder{

TextView title;

TextView namaPerusahaan;

TextView lokasi;

TextView kriteria_1;

TextView kriteria_2;

TextView kriteria_3;

TextView gaji;

ImageView img_logo;

TextView tanggal;

public ViewHolder(final View itemView, final Context ctx) {

super(itemView);

context = ctx;

title = itemView.findViewById(R.id.titleID);

namaPerusahaan = itemView.findViewById(R.id.namaPerusahaanID);

kriteria_1 = itemView.findViewById(R.id.satuTv);

kriteria_2 = itemView.findViewById(R.id.duaTv);

kriteria_3 = itemView.findViewById(R.id.tigaTv);

gaji = itemView.findViewById(R.id.gajiID);

lokasi = itemView.findViewById(R.id.lokasiID);

img_logo = itemView.findViewById(R.id.img_logoID);

tanggal = itemView.findViewById(R.id.tanggalID);

itemView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

HomeFragmentDetail fragmentDetail = new HomeFragmentDetail();

Bundle bundle = new Bundle();

Job job = jobList.get(getAdapterPosition());

bundle.putSerializable("job", job);

fragmentDetail.setArguments(bundle);

AppCompatActivity activity = (AppCompatActivity) itemView.getContext();

android.support.v4.app.FragmentManager fragmentManager = activity.getSupportFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().addToBackStack(null);

fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

fragmentTransaction.replace(R.id.frame_fragmenthome, fragmentDetail);

fragmentTransaction.commit();

//// Intent intent = new Intent(context, HomeDetailActivity.class);

//// intent.putExtra("job", job);

//// ctx.startActivity(intent);

}

});

}

}

}

HomeFragmentDetail.java

package com.mimdudin.carekkerje.Activities;

import android.content.Context;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import com.mimdudin.carekkerje.Model.Job;

import com.mimdudin.carekkerje.R;

/**

* A simple {@link Fragment} subclass.

* Activities that contain this fragment must implement the

* to handle interaction events.

*/

public class HomeFragmentDetail extends Fragment {

private TextView tvDeskripsi;

private TextView tvIndustri;

public HomeFragmentDetail() {

// Required empty public constructor

}

public static HomeFragmentDetail newInstance(){

HomeFragmentDetail homeFragmentDetail = new HomeFragmentDetail();

return homeFragmentDetail;

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_home_fragment_detail, container, false);

tvDeskripsi = view.findViewById(R.id.tvDeskripsi);

tvIndustri = view.findViewById(R.id.tvIndustri);

Bundle bundle = getArguments();

Job job = (Job) bundle.getSerializable("job");

String PostID = job.getId_post();

String titleID = job.getTitle();

tvDeskripsi.setText(PostID);

tvIndustri.setText(titleID);

return view;

// DataHolder dataholder=new DataHolder("1","TestName");

// Bundle bundle=new Bundle();

// bundle.putSerializable("obj",dataholder);

// Fragment fragment=new FragmentB();

// fragment.setArguments(bundle);

// fragmentManager = getActivity(). getSupportFragmentManager();

// fragmentTransaction = fragmentManager .beginTransaction();

// fragmentTransaction.add(R.id.container, fragment);

// fragmentTransaction.commit();

//Job job = (Job) getIntent.getSerializableExtra("job");

// Bundle bundle = getArguments();

// Job job = (Job) bundle.getSerializable("job");

// String jobID = job.getId_post();

// String titleID = job.getTitle();

}

}

AppConfig.java

package com.mimdudin.carekkerje.Util;

/**

* Created by master on 10/12/17.

*/

public class AppConfig {

public static final String URL = "http://192.168.43.142/Carekkerje/v1";

public static final String URL_REGISTER = URL + "/daftarUser";

public static final String URL_LOGIN = URL + "/loginUser";

public static final String URL_GETJOBS = URL + "/seluruhLowongan/";

public static final String URL_GETJOBSS = URL + "/seluruhLowongan/:title";

}

以上是 android - 用户输入的URL编码 的全部内容, 来源链接: utcz.com/qa/260782.html

回到顶部