-1

I get Data from Json in android,date get and save in String Variable.but when use DecodeUrl its error:

Error: java.lang.IllegalArgumentException: Invalid % sequence at 40:

my code:

@SuppressLint("NewApi")
    public String JsonReguest(String url) {
        String json = "";
        String result = "";
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object

        HttpGet httpget = new HttpGet(url);
        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Content-Type", "application/json");


        HttpResponse response;
        try {


            response = httpclient.execute(httpget);
            response.setHeader("Content-Type","UTF-8");
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();

                result = convertStreamToString(instream);
                InputStream stream = new ByteArrayInputStream(result.getBytes("UTF-8"));
                result = convertStreamToString(stream);

                // String encode_url=URLEncoder.encode(result,"UTF-8");
                // String decode_url=URLDecoder.decode(encode_url,"UTF-8");

                //result=decode_url;
                //String decodedUrl = URLDecoder.decode(result, "UTF-8");
                result=URLDecoder.decode(result);

            }
        } catch (Exception e) {
            Log.e("Error", e.toString()); 
        }
        return result;
    }

    public static String convertStreamToString(InputStream is) {


        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

simple text of json :

{"CategoryID":11,"ParentID":0,"Title":"%u062E%u0648%u062F%u0631%u0648","PicAddress":""},{"CategoryID":16,"ParentID":0,"Title":"%u0627%u0645%u0644%u0627%u0643%20","PicAddress":""}

this line crashed : result=URLDecoder.decode(result);

how to Resolve Problems.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
sr.farzad
  • 665
  • 3
  • 8
  • 23
  • 1
    Ugh! those lines blinded me ... first do not play with `StrictMode` - use `AsyncTask` ... next: instead of "reading stream twice"(i know why you are doing this - to apply utf-8 encoding but ...) use `result = EntityUtils.toString(entity, "utf-8");` ... and finally: do you really wana use `URLDecoder` to parse json? – Selvin Sep 20 '13 at 14:42
  • all data is Encode from server thus i must Decode UTF-8 – sr.farzad Sep 20 '13 at 14:46
  • you know that `InputStreamReader` has also this constructor: `public InputStreamReader(InputStream in, String charsetName)` so replacing `new InputStreamReader(is)` with `new InputStreamReader(is, "utf-8")` would "decode" response in one pass ... but using `EntityUtils.toString` is easier – Selvin Sep 20 '13 at 14:48
  • 1
    anyway ... `%uxxxx` is not a standard `x-www-form-urlencoded` so i really don't know if `URLDecoder` can handle such thing – Selvin Sep 20 '13 at 15:02

2 Answers2

2

first decode specifing your encoding

String result = URLDecoder.decode(url, "UTF-8");

and then go to http://json.org/, scroll down and choose one of the supported json parsing Java libraries

Emanuele
  • 469
  • 2
  • 10
2

As Selvin commented %uxxxx is not a standard Url encoded string , so it's obvious to get an error

you have 2 options:

  1. Contact the service provider to fix her url encoded strings and use URLDecoder.decode in your code
  2. write a custom decoder for such strings

P.S. ask your questions more clear to avoid getting negative points

mjalil
  • 341
  • 3
  • 16