简体   繁体   中英

Escaping quotes in java

I'm trying to make a webservice call where I have to pass

login.php?message=[{"email":"mikeymike@mouse.com","password":"tiger"}]

I've use backslash to escape the double quotes like this

String weblink = "login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]";

But I'm still getting errors. I've tried making calls to other webservices which don't have require data with any double quotes and they work fine so I'm quite sure the problem is from this. Also I get a java.lang Exception saying

java.lang.Exception  Indicates a serious configuration error.DateParseException An exception to indicate an error parsing a date string.  DestroyFailedException Signals that the destroy() method failed         

EDIT: I've tried using URLEncoder and JSON object but still get an error

Here is the rest of the code

String HOST = "http://62.285.107.329/disaster/webservices/";

 String weblink = "login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]";
String result = callWebservice(weblink);

 public String callWebservice(String weblink) {
    String result = "";
    try {

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 7500;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        int timeoutSocket = 7500;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient client = new DefaultHttpClient(httpParameters);
        HttpGet request = new HttpGet();
        URI link = new URI(HOST + weblink);
        request.setURI(link);
        HttpResponse response = client.execute(request);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        result = rd.readLine();

    } catch (Exception e1) {
        e1.printStackTrace();
        result = "timeout";
    }
    return result;
}

Also the webservice returns a JSON object so could this also be a reason for the error?

Instead of trying this by hand and getting errors, why don't use use a combination of the JSONObject class and UrlEncoder.

 JSONObject json = new JSONObject();
 json.put("email","mikeymike@mouse.com" );
 json.put("password", "tiger");
 String s = "login.php?message=" + UrlEncoder.encode(json.toString());

You have to use %22 in place of " as in: login.php?message=[{%22email%22:%22mikeymike@mouse.com%22,%22password%22:%22tiger%22}]

" is not a valid character in an URL.

A more general solution is to use URLEncoder.encode("login.php?message=[{\\"email\\":\\"mikeymike@mouse.com\\",\\"password\\":\\"tiger\\"}]", "UTF8")

When you communicate with web services you need to URL encode your data. In your case, the url encoding would replace " with %22 , but if you were to add other special characters, such as % , the encoding would capture these as well. There is a java class in the JDK for this, called URLEncoder .

So, basically, what you would do is to prepare your string using URLEncoder.encode() , like so:

String weblink = URLEncoder.encode("login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]");

Now that the string is encoded, you should be able to send it along to the server and have it understand what you mean.

Apologies to all but it seems the problem was that I was trying to consume the webservice in the wrong way as it returns a JSON object.

The proper way to do this for anyone who might come across this is in the code below

String str="url";
try{
    URL url=new URL(str);
    URLConnection urlc=url.openConnection();
    BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream()));
    String line;
    while((line=bfr.readLine())!=null)
    {
    JSONArray jsa=new JSONArray(line);
    for(int i=0;i<jsa.length();i++)
       {
       JSONObject jo=(JSONObject)jsa.get(i);
                    title=jo.getString("deal_title");  //tag name "deal_title",will return value that we save in title string
                des=jo.getString("deal_description");
   }
}
catch(Exeption e){
}

This answer was gotten from

How to call a json webservice through android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM