【问题标题】:Code in FOR loop not executedFOR循环中的代码未执行
【发布时间】:2012-11-06 20:38:41
【问题描述】:

我有一个 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();
            }

显示你好日志。 显示 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 循环的任何帮助。 感谢您的帮助。

【问题讨论】:

    标签: android gson progressdialog


    【解决方案1】:

    请提供有关为什么不执行 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,但是还有其他的例子来说明如何读取列表:

    【讨论】:

    • 但该行中的数据不为空:this.response = gson.fromJson(inputStreamReader, Response.class); 否?所以那里有“调查”对象。
    • 这肯定是一个盲目的猜测:D 因为我无法从 Response 转换为 ArrayList。但是请看 Response 构造函数,当有响应实例时,data arraylist 不为空,它将存储调查数据。
    • 在 Response 的构造函数中,您使用 new ArrayList&lt;Survey&gt;(); 创建了一个空列表,但您从未向它传递任何数据...
    • 对不起,山姆,但我不明白该代码的用法?在数据中存储“调查”?谢谢。
    • SAM 我在一个完全相同的其他项目中使用了那个 url php 文件,并且运行良好 http://api.androidsmith.com/capitals.php。我认为执行 php 脚本时不会检索数据。请看看我是如何执行我的 php 文件的,也许有问题。
    【解决方案2】:

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

    【讨论】:

    • 目前尚不清楚引用执行阶段更新的实例变量是否有效。它不是 AsyncTask 类特别允许的操作之一。您可以将执行阶段的结果定义为调查,并将作为 onPostExecute 的参数显示。
    • 有什么代码建议吗?因为似乎 data 变量由于某种原因是空的。
    • 抱歉,我不太擅长 JSON。我会打印出 JSON 数据,看看它是什么样子的。
    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2014-06-02
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多