-1

I try to implement Android searchable and I want to filter query, I follow this link, this, and others. but in Android Studio I got this message unhandled exception java.io.UnsupportedEncodingException, this is my code

import java.net.URLEncoder;`
private void doSearch(String queryStr) {
    // get a Cursor, prepare the ListAdapter
    // and set it
    //Log.e("Query",queryStr);
    searchRestaurants(URLEncoder.encode(queryStr, "UTF-8"));}
Community
  • 1
  • 1
Latief Anwar
  • 1,833
  • 17
  • 27

2 Answers2

2

You need to wrap your URLEncoder.encode()-method in a try-catch block:

try {
  URLEncoder.encode(queryStr, "UTF-8");
} catch (UnsupportedEncodingException e) {
  Log.e("Yourapp", "UnsupportedEncodingException");
}

The reason you're getting this error is that some platforms might not support UTF-8 encoding. Android definitely does, so you'll never receive this Exception, but you still need to handle it to make the compiler happy.

However, your code won't do anything, you'll need to store the result of the encode()-operation in a variable, e.g. String myEncodedQuery = URLEncoder.encode(queryStr, "UTF-8");.

Miraduro
  • 229
  • 1
  • 9
  • Wat? I literally handed the code to the OP on a silver platter. I'm happy to provide a link how to perform copy/paste operations on various operating systems, too. – Miraduro Apr 16 '17 at 18:37
  • I do not know about try catch, where i write next code, `URLEncoder.encode(queryStr, "UTF-8"); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); RestaurantsRetrofit request = retrofit.create(RestaurantsRetrofit.class); Call call = request.searchRestaurants(queryStr)` – Latief Anwar Apr 16 '17 at 18:38
  • Please update your question if you're not actually asking about the problem you've formulated in your question. – Miraduro Apr 16 '17 at 18:39
  • 1
    This answer not help. – Latief Anwar Apr 16 '17 at 18:47
  • My answer fixes the problem that you've stated in your question. If you have a different problem, please update your question or post a new question. – Miraduro Apr 16 '17 at 19:19
-1
import java.net.URLEncoder;
private void doSearch(String queryStr) {
    // get a Cursor, prepare the ListAdapter
    // and set it
    //Log.e("Query",queryStr);
    try {
        final String encodedPath = URLEncoder.encode(queryStr, "UTF-8"));
        searchRestaurants(encodedPath);
    } catch (UnsupportedEncodingException ec) {
      Log.d(TAG, ec.printStacktrace);
    }
}
JeffNhan
  • 363
  • 3
  • 3