Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Android parse JSON using Post method with email and password

I have never before been working with such requests, and I don't know what to search for in the internet... So I have this task, I have an api url - https://www.myurl.com/oauth2/2.0/signin And it should be provided with an email and password. The url accepts post method (which I kind of don't know what it means) and if successful, should provide a JSON Object, but I don't know how to provide the email and password in the link... I have this AsyncTask

public class JSONGet extends AsyncTask<String, Void, String> {

String adres;
JSONObject jsonGet;
JSONParserCallback delegate;

public JSONGet(JSONParserCallback callback, String adres) {
    this.delegate = callback;
    this.adres = adres;
}

@Override
protected String doInBackground(String... params) {
    try {
        HttpClient client = new DefaultHttpClient();
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("[email protected]", "android"));
        HttpPost httppost = new HttpPost("https://www.myurl.com/oauth2/2.0/signin");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse hr = client.execute(httppost);
        int status = hr.getStatusLine().getStatusCode();
        if (status == 200) {
            HttpEntity he = hr.getEntity();
            String data = EntityUtils.toString(he);
            jsonGet = new JSONObject(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    delegate.onFinishedParsing(jsonGet);
}

}

Upvotes: 0

Views: 234

Answers (1)

Prerak Sola
Prerak Sola

Reputation: 10019

You can provide username and password like this:

    nameValuePairs.add(new BasicNameValuePair("userName","{username string}"));
    nameValuePairs.add(new BasicNameValuePair("password","{password string}"));

Replace, {username string} and {password string} with your username and passoword.

Upvotes: 3

Related Questions