1

Facebook server side login sends response in the format:

access_token=USER_ACCESS_TOKEN&expires=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES

Is there a way to parse this conveniently in Java, other than writing custom code? Something like parse_str in PHP.

EDIT: This question has nothing to do with query string or request, It is about parsing request body in the above format.

3
  • Even if it weren't closed as "duplicate", it doesn't say enough of what you want to do. Can you expand on what you want to do specifically. Start by provide examples of original data, and how you want it to look. Commented Apr 30, 2013 at 18:16
  • The question is quite simple. I want to parse an HTTP response where the body of the response is in the given format. Obviously I can write custom code as @gerrytan suggested, but I want to know if there is some Java class that already handles this parsing. Commented Apr 30, 2013 at 18:24
  • 1
    Your comment makes sense, but your question doesn't; as a suggestion: I think you should reword your question to match what you've said in the comment above. Now, to answer your question: No, there doesn't seem to be any built-in class/functions that will accomplish this, however (!), there is an Apache HTTPMessageParser which seems to do it. Commented Apr 30, 2013 at 18:42

2 Answers 2

2

You can write a simple parser using string split method and a map, something like this:

Map<String,String> fbParam = new HashMap<String,String> ();
String[] pairs = fbResp.split("&");
for(String pair : pairs) {
  String[] keyval = pair.split("=");
  fbParam.put(keyval[0], keyval[1]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually that is what I would do, but it seems strange there's no in-built way to do this.
0

If you're in a Java EE environment, you could use ServletRequest.getParameter().

You might also consider using the Web4J Framework's request parser.

1 Comment

I want to do this for response, not request

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.