Aashish Dahal
Aashish Dahal

Reputation: 13

Catching JSON response form web post request in android

I have been working with WebView in android for loading HTML files in android. On my way, I am trying to test an API service that will let me access the services if I provide required parameter and responds in JSON. My question is, How can I parse the obtained JSON from the FORM POST request in android and reuse the resultants.

My HTML for form with post method is provided below:

     <form method="post" action="htts://www.ipay.com.np/api/v2/recharge">
    <div class="col-md-6">

    <div class="form-group">
      <label for="email">Username:</label>
      <input type="email" name="username" class="form-control" id="username" placeholder="Enter username">
    </div>
  </div>

  <div class="col-md-6">

    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" name="password" class="form-control" id="pwd" placeholder="Enter password">
    </div>
  </div>

  <div class="col-md-12">

  <div class="form-group">
    <label for="choose">Amount:</label>
    <select name="mid"  class="form-control" id="mid" placeholder="Query">
      <option value="null">what-would-you-like-to-do</option>
      <option value="37">NTC-POSTPAID RECHARGE</option>
      <option value="35">NTC LANDLINE PSTN</option>
      <option value="20">NCELL AIRTIME</option>
      <option value="36">NTC PREPAID</option>
      <option value="33">ADSL UNLIMITED</option>
      <option value="39">ADSL VOLUME</option>
      <option value="46">UTL TOPUP</option>

  </select>
  </div>
</div>

  <div class="col-md-6">

    <div class="form-group">
      <label for="phone">Phone Number:</label>
      <input type="text" name="phone" min="1" class="form-control" id="phone" placeholder="Enter phone number">
    </div>
  </div>
  <div class="col-md-6">

    <div class="form-group">
      <label for="amount">Amount:</label>
      <select name="amount"  class="form-control" id="email" placeholder="Enter email">
        <option value="null">select-amount</option>
        <option value="Rs.10">10</option>
        <option value="Rs.20">20</option>
        <option value="Rs.30">30</option>
        <option value="Rs.40">40</option>
        <option value="Rs.50">50</option>
        <option value="Rs.100">100</option>
        <option value="Rs.100+">100+</option>
    </select>
    </div>
  </div>

  <div class="col-md-6">

    <div class="checkbox">
      <label><input type="checkbox"> Remember me</label>
    </div>
  </div>
</div>

    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>

And the responce example i get from the following POST method is:

    {

"txn_no" : 12343,

"ord_no" : 1222,

"status" : "paid",

"merchant" : "Ncell Air time",

"description" : "recharge of...",

"amount" : 20.00, "reward" : 0.02

}

It would be very helpfull, if i only get idea to catch the json response in my android application.

Upvotes: 0

Views: 445

Answers (1)

Jaimin Prajapati
Jaimin Prajapati

Reputation: 341

There is no need to use webview for its, you can achieve its by HttpPost in activity,

public boolean validateLogin()
            {
                try {
                    progressBar.setVisibility(View.VISIBLE);
    //call webservice and get json response
                    String JSONStr = PostData();
                    jsonObject = new JSONObject(JSONStr);
                    String status = jsonObject.get("success").toString();
                    return status.equals("1");
                }
                catch (Exception e)
                {
                    return false;
                }
            }
             public String PostData() {
                    String jsonResponse = "";
                    String URL = "https://example.com/Login.php?Email=" + str_email + "&Password=" + str_password;
                    try {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpPost httpPost = new HttpPost(URL);

                        HttpResponse httpResponse = httpClient.execute(httpPost);

                        HttpEntity httpEntity = httpResponse.getEntity();
                        jsonResponse = readResponse(httpResponse);
                    } catch (Exception exception) {
                    }
                    return jsonResponse;
                }

                public String readResponse(HttpResponse res) {
                    InputStream is = null;
                    String return_text = "";
                    try {
                        is = res.getEntity().getContent();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
                        String line = "";
                        StringBuffer sb = new StringBuffer();
                        while ((line = bufferedReader.readLine()) != null) {
                            sb.append(line);
                        }
                        return_text = sb.toString();
                    } catch (Exception e) {

                    }
                    return return_text;

                }

Upvotes: 1

Related Questions