2

I have created an android app in which I had made the http request here I want to know how can I read the response from the server and show error and success message after returning string builder:

        try{
            HttpResponse httpResponse = httpclient.execute(post);
            InputStream inputStream = httpResponse.getEntity().getContent();

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuilder stringBuilder = new StringBuilder();

            String bufferedStrChunk = null;

            while((bufferedStrChunk = bufferedReader.readLine()) != null){
                stringBuilder.append(bufferedStrChunk);
            }
            return stringBuilder.toString();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    //}catch(Exception e){
     //  e.printStackTrace();
   //}
    //return 0L;
    return null;
}

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(String result) {

}

}

here i want to show error and success message after returning string builder .

Moulick
  • 45
  • 5
  • `StrictMode.ThreadPolicy policy`. Terrible. Remove all StrictMode code from that task. Remove all StrictMode code from your project. – greenapps Jul 17 '16 at 08:19
  • can u suggest or send a code for that section if possible – Moulick Jul 17 '16 at 08:32
  • Why? Your code worked wasnt it? You only aded some code for reading the returned page. So you should report if you get the page now. – greenapps Jul 17 '16 at 08:33
  • every thing is working i have added the code Input and output stream to get the response as if any of the error occurs so it will show me what the error is ,it is to get the response that if there is any false data – Moulick Jul 17 '16 at 08:40
  • Well if all works then you are ready. But remove all strict mode!!! – greenapps Jul 17 '16 at 08:49
  • But the thing is that I want to get response in console that for which parameter what data has been send or if the data is not send what is the issue it show me in console . – Moulick Jul 17 '16 at 09:10
  • Console? Where are you talking about? And i dont see you doing anything with that page. How did you check that you got a page and that the page was ok? – greenapps Jul 17 '16 at 09:15
  • I only want to know that how can I read the Response of the http request and what are the codes i should include in that because i am not getting any response of the request – Moulick Jul 17 '16 at 09:33
  • You are already reading the response. You have added that code. So what are you talking about? `return stringBuilder.toString();` contains the returnd page. – greenapps Jul 17 '16 at 15:45
  • i was not getting the response thats why i have asked – Moulick Jul 17 '16 at 16:06
  • Please answer to the point. What does stringBuilder.toSting() contain? And explain how your code flows. Follow the statements. Tell us what happens and nkt happens. – greenapps Jul 17 '16 at 16:51
  • @greenapps: how to add the success and error message after returning srtingbuilder – Moulick Jul 31 '16 at 11:06

1 Answers1

2
                        String response = null;
                        try {
                            response = SimpleHttpClient
                                    .executeHttpPost("url",
                                            postParameters);
                             res = response.toString();

                             return res;

                        } catch (Exception e) {
                            e.printStackTrace();
                            errorMsg = e.getMessage();
                        }
                    }
                }).start();
                try {
                    Thread.sleep(3000);


                //  error.setText(resp);
                    if (null != errorMsg && !errorMsg.isEmpty()) {

                    }
                } catch (Exception e) {
                }

            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            if(regDialog!=null)
            {

                regDialog.dismiss();

            //do you code here you want

                }

  // do what u do
    }
Moulick
  • 45
  • 5
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
  • How should I include List nameValuePairs = new ArrayList in that Private class because without that nothing is happening – Moulick Jul 16 '16 at 12:17
  • you can add json in Async tast like as you add the numvalue – Arjun saini Jul 16 '16 at 12:24
  • quite understand but too much complex where is the RES stared and ERROEMESSAGE stated – Moulick Jul 16 '16 at 13:26
  • Very bad code. Dont start a thread in doIBackground. DoInBackground is a thread already. – greenapps Jul 16 '16 at 16:26
  • I wonder why you posted all the code again with a lot of bad code added. The original code was ok. You only had to add reading the returned page as asked. – greenapps Jul 16 '16 at 16:29
  • Ya actually that code was complex for me to apply as being new to this was getting me very confused have edited the code again in my actual code please have a look on it what changes should I do or please try to edit it I have edited the code in the SENDMESSAGE class and shifted the nameValuePair form onClick section to SENDMESSAGE section and have initialise the JSONObject @greenapps – Moulick Jul 17 '16 at 04:06
  • @greenapps I am new in android I will to make code easily and make short.....and also try to Answer on well question not again......But this is working for me... – Arjun saini Jul 18 '16 at 07:31
  • Here SimpleHttpClient is class which handle to request to server and encode the data..... and errormsg is a string...... you declare it or remove from code..... – Arjun saini Jul 18 '16 at 09:44
  • @Moulick you edit the full code......In my COde without simpleHttpClient you not able to post... – Arjun saini Jul 18 '16 at 09:48