FOR循环中的代码未执行

时间:2012-11-18 22:01:03

标签: android gson progressdialog

我有一个ProgressDialog,它通过执行php脚本从数据库中检索后台数据。 我正在使用gson Google库。从浏览器执行时,php脚本运行良好:

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

然而,ProgressDialog背景处理效果不佳:

@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。 显示STRINGBUILDER日志。 问题日志不会显示。 显示OUT FOR Log。

调查班:

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;
    }


}

响应类:

public class Response {

    ArrayList<Survey> data;

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

有关为什么不执行FOR循环的任何帮助。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

  

任何帮助请关注为什么不执行FOR循环。

简单地说:data是空的。 (所以循环没有任何东西可以迭代......)

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);

我自己没有使用过GSON,但还有其他如何阅读列表的例子:

答案 1 :(得分:0)

你的onPostExecute接受一个名为result的参数。 for循环遍历名为response的实例变量中的元素。它们应该是一样的吗?