Search code examples
javaandroidurlescapingjsonresult

extra \ in json response before every / in java, android


My code to get image Urls

DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }
        inputStream.close();
        return stringBuilder.toString();

where server code is in php

But problem is there is extra \ before every / e.g. in database image Url is, http://www.dvimaytech.com/markphoto/upload/[email protected]/Pic.jpg but I get every time http:\/\/www.dvimaytech.com\/markphoto\/upload\/[email protected]\/Pic.jpg

Is this problem isn't solvable, then another solution(its last option for me) is to remove every .

But when I try that using url = url.replace("\",""); it gives syntax error String literal is not properly closed by a double-quote


Solution

  • Just use a JSON parser library like gson to decode your JSON packets for you. http://code.google.com/p/google-gson/

    It will make your life much easier and avoid having to string.replace() specific characters.