0

我有一个 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 日志。显示 STRINGBUILDER 日志。问题日志不显示。显示 OUT FOR 日志。

调查类:

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 循环的任何帮助。谢谢你的帮忙。

4

2 回答 2

2

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

简单地说:data是空的。(所以循环没有什么可以迭代的......)

从GSON 的文档中尝试这样的事情:

 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,但是还有其他关于如何阅读列表的示例:

于 2012-11-18T22:03:21.657 回答
0

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

于 2012-11-18T22:05:55.203 回答