-1

I am trying to send some data on an API. However my APP isn't giving any response. Neither giving any error nor getting stopped. Its like nothing just happened. I am bit new to android so kindly guide. Besides I know HTTPClient and other imports have become deprecated.

public void makePostRequest()
{
    class makePostRequestAsyncTask extends AsyncTask<Void, Void, String> {


        @Override
        protected String doInBackground(Void... params) {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://103.226.216.209/finger_varification/api/finger_verification");
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
            nameValuePair.add(new BasicNameValuePair("sec_key", "2af8b9d956c39d2d52c46c2a02a978d1"));
            nameValuePair.add(new BasicNameValuePair("cnic", "3520214037921"));
            nameValuePair.add(new BasicNameValuePair("imei_no", "864121017028886"));
            nameValuePair.add(new BasicNameValuePair("device_name", "FingerMap5"));
            nameValuePair.add(new BasicNameValuePair("finger_index",index ));
            nameValuePair.add(new BasicNameValuePair("finger_template", "sdsd"));//model1.toString()));
            nameValuePair.add(new BasicNameValuePair("template_type", "RAW_IMAGE"));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e) {
                // log exception
                e.printStackTrace();
            }


            //making POST request.
            try {
                HttpResponse response = httpClient.execute(httpPost);
                // write response to log

                Log.d("Http Post Response:", response.toString());
            } catch (ClientProtocolException e) {
                // Log exception
                e.printStackTrace();
            } catch (IOException e) {
                // Log exception
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if(result.equals("working")){
                Toast.makeText(getApplicationContext(), "HTTP POST is working...", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Invalid POST req...", Toast.LENGTH_LONG).show();
            }
        }


    }

}

1 Answers1

0

This is how you get the body of the response

HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String response_body = reader.readLine();
Log.d("Http Post Response:", response_body);

By the way, HttpPost and HttpResponse is deprecated on API 23, use HttpURLConnection instead

NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
  • Exactly deprecated means? I guess it will work. What do you think? moreover...I am not sure how to send and receive using HttpURLConnection....since i have 7 strings to send. – Muhammad Abdullah Oct 22 '15 at 12:23
  • @MuhammadAbdullah: Meaning of deprecated is explained here http://stackoverflow.com/questions/8111774/deprecated-meaning Short story, it won't be supported for further development/bug fixes in versions after the API 23. Hence you better use HttpURLConnection as mentioned by NaviRamyle – Araw Oct 22 '15 at 12:27
  • @MuhammadAbdullah, in regards of HttpURLConnection, I took this piece of code from my personal small projects. `URL url = new URL(uri[0]); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); // The request will automatically be fired on demand when you want to get // any information about the HTTP response httpURLConnection.connect(); int statusCode = httpURLConnection.getResponseCode();` – AuroMetal Oct 22 '15 at 12:33
  • `if (statusCode == 200) { BufferedReader inbf = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = inbf.readLine()) != null) { response.append(inputLine); } responseString = response.toString(); inbf.close(); }` – AuroMetal Oct 22 '15 at 12:33