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.

arahant
  • 2,203
  • 7
  • 38
  • 62
  • 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. – Jesse Apr 30 '13 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. – arahant Apr 30 '13 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](http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/io/HttpMessageParser.html) which seems to do it. – Jesse Apr 30 '13 at 18:42

2 Answers2

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]);
}
gerrytan
  • 40,313
  • 9
  • 84
  • 99
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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684