0

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.

androniennn
  • 3,117
  • 11
  • 50
  • 107

2 Answers2

2

Any help please concerning WHY the FOR loop is not executed.

Simply put: data is empty. (So there is nothing for the loop to iterate over...)

Try something like this, from GSON's documentation:

 Type listType = new TypeToken<List<String>>() {}.getType();
 List<String> target = new LinkedList<String>();
 target.add("blah");

 Gson gson = new Gson();
 String json = gson.toJson(target, listType);
 List<String> target2 = gson.fromJson(json, listType);

I haven't used GSON myself, but there are other examples of how to read lists:

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • but data become not empty in that line : `this.response = gson.fromJson(inputStreamReader, Response.class);` no? So there is "surveys" objects there. – androniennn Nov 18 '12 at 22:09
  • That surely was a blind guess :D because i can't convert from Response to ArrayList. But please look at Response constructor, when there is a response instance, data arraylist is not empty, it will stores surveys data. – androniennn Nov 18 '12 at 22:13
  • 1
    In Response's constructor, you create an empty list with `new ArrayList();` but you never pass it any data... – Sam Nov 18 '12 at 22:16
  • Sorry sam but i do not understand the use of that code? To store "surveys" in data? thank you. – androniennn Nov 18 '12 at 22:17
  • SAM i used that url php file in an exactly same other project and that's working well `http://api.androidsmith.com/capitals.php`. I think that data is not retrieved when executing php script. Please see how i execute my php file perhaps there is something wrong. – androniennn Nov 18 '12 at 22:22
  • I'm reading about GSON's `fromJSON()` since I haven't worked with it. From what I can tell your class `Survey` does not match the JSON element `"surveys"`, change the JSON element to match the class name. – Sam Nov 18 '12 at 22:42
  • But it's matching the class name. No? I'm not using "surveys" anywhere. – androniennn Nov 18 '12 at 22:46
  • You are using "surveys" here: `{"surveys":[{"id_survey":"1"...` and `Survey` does not match `surveys`. – Sam Nov 18 '12 at 22:47
  • `surveys` is the table name in database. – androniennn Nov 18 '12 at 22:49
  • So i rename class Survey to Surveys. Or i change table database name to "Survey". That's what you'd like to tell please? – androniennn Nov 18 '12 at 22:52
0

Your onPostExecute takes in a parameter called result. Your for loop iterates over the elements in an instance variable called response. Are they supposed to be the same?

emrys57
  • 6,679
  • 3
  • 39
  • 49
  • 1
    It's not clear that referring to instance variables updated by the execute phase is going to work. It's not one of the specifically allowed operations of the AsyncTask class. You could define the result of the execute phase as a Survey and that would appear as the parameter to onPostExecute. – emrys57 Nov 18 '12 at 22:17
  • Any code suggestion please? Because it seems that data variable is empty for some reason. – androniennn Nov 18 '12 at 22:25
  • Sorry, I'm not much good at JSON. I'd print out the JSON data and see what it looks like. – emrys57 Nov 18 '12 at 22:30