Code in FOR loop not executed

Posted by androniennn on Stack Overflow See other posts from Stack Overflow or by androniennn
Published on 2012-11-18T22:01:02Z Indexed on 2012/11/18 23:00 UTC
Read the original article Hit count: 288

Filed under:
|
|

I have a ProgressDialog that retrieves in background data from database by executing php script. I'm using gson Google library. php script is working well when executed from browser:

{"surveys":[{"id_survey":"1","question_survey":"Are you happy with the actual government?","answer_yes":"50","answer_no":"20"}],"success":1}

However, ProgressDialog background treatment is not working well:

@Override
        protected Void doInBackground(Void... params) {
            String url = "http://192.168.1.4/tn_surveys/get_all_surveys.php";

            HttpGet getRequest = new HttpGet(url);
            Log.d("GETREQUEST",getRequest.toString());

            try {

                DefaultHttpClient httpClient = new DefaultHttpClient();
                Log.d("URL1",url);

                HttpResponse getResponse = httpClient.execute(getRequest);
                Log.d("GETRESPONSE",getResponse.toString());
                final int statusCode = getResponse.getStatusLine().getStatusCode();
                Log.d("STATUSCODE",Integer.toString(statusCode));
                Log.d("HTTPSTATUSOK",Integer.toString(HttpStatus.SC_OK));
                if (statusCode != HttpStatus.SC_OK) { 
                    Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); 
                    return null;
                }

                HttpEntity getResponseEntity = getResponse.getEntity();
                Log.d("RESPONSEENTITY",getResponseEntity.toString());
                InputStream httpResponseStream = getResponseEntity.getContent();
                Log.d("HTTPRESPONSESTREAM",httpResponseStream.toString());
                Reader inputStreamReader = new InputStreamReader(httpResponseStream);

                Gson gson = new Gson();
                this.response = gson.fromJson(inputStreamReader, Response.class);

            } 
            catch (IOException e) {
                getRequest.abort();
                Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
            }

            return null;
        }
    @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                Log.d("HELLO","HELLO");
                StringBuilder builder = new StringBuilder();
                Log.d("STRINGBUILDER","STRINGBUILDER");
                for (Survey survey : this.response.data) {
                    String x= survey.getQuestion_survey();
                    Log.d("QUESTION",x);
                    builder.append(String.format("<br>ID Survey: <b>%s</b><br> <br>Question: <b>%s</b><br> <br>Answer YES: <b>%s</b><br> <br>Answer NO: <b>%s</b><br><br><br>", survey.getId_survey(), survey.getQuestion_survey(),survey.getAnswer_yes(),survey.getAnswer_no()));

                }

                Log.d("OUT FOR","OUT");
                capitalTextView.setText(Html.fromHtml(builder.toString()));
                progressDialog.cancel();
            }

HELLO Log is displayed. STRINGBUILDER Log is displayed. QUESTION Log is NOT displayed. OUT FOR Log is displayed.

Survey Class:

public class Survey {

    int id_survey;
    String question_survey;
    int answer_yes;
    int answer_no;

    public Survey() {

        this.id_survey = 0;
        this.question_survey = "";
        this.answer_yes=0;
        this.answer_no=0;
    }

    public int getId_survey() {
        return id_survey;
    }

    public String getQuestion_survey() {
        return question_survey;
    }

    public int getAnswer_yes() {
        return answer_yes;
    }

    public int getAnswer_no() {
        return answer_no;
    }


}

Response Class:

public class Response {

    ArrayList<Survey> data;

    public Response() {
        data = new ArrayList<Survey>();
    }
}

Any help please concerning WHY the FOR loop is not executed. Thank you for helping.

© Stack Overflow or respective owner

Related posts about android

Related posts about gson