gamo
gamo

Reputation: 1577

How to get info from HttpResponse java?

First of all please look at my code below:

    List<BasicNameValuePair> qsList = new ArrayList<BasicNameValuePair>();
    qsList.add(new BasicNameValuePair("oauth_token", accessToken));
    String queryString = URLEncodedUtils.format(qsList, HTTP.UTF_8);
    HttpGet userInfoRequest = new HttpGet(id + "?" + queryString);
    DefaultHttpClient defaultHttpClientclient = new DefaultHttpClient();

    HttpResponse userInfoResponse;
    try {
        userInfoResponse = defaultHttpClientclient.execute(userInfoRequest);
        String responseBody = EntityUtils.toString(userInfoResponse.getEntity());
        System.out.println("User info response: " + responseBody);
        System.out.println("");
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I got an access token from Salesforce. Now I request user's information via that token. In the responseBody I got all infomation of that user like username, id, language,... Now I need to take only username from the response. What should I do to take it?

Upvotes: 0

Views: 953

Answers (1)

ryandlf
ryandlf

Reputation: 28555

The response is likely in JSON. If so you can parse the data you need. I won't repost the code, instead please see: How to parse JSON in Java

    JSONObject responseJSON = new JSONObject(EntityUtils.toString(userInfoResponse.getEntity());
    String username = responseJSON.getString("username");

Upvotes: 1

Related Questions