Android 单文本视图中的多行数据

Android 单文本视图中的多行数据,android,mysql,json,Android,Mysql,Json,下面是我多次尝试使用json在单个文本视图中获取多行值的代码 protected Void doInBackground(Void... arg0) { HttpClient myClient = new DefaultHttpClient(); HttpPost myConnection = new HttpPost("http://192.168.1.5/send-data.php"); try {

下面是我多次尝试使用json在单个文本视图中获取多行值的代码

protected Void doInBackground(Void... arg0)
        {

            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new HttpPost("http://192.168.1.5/send-data.php");

            try {
                response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try{
                JSONArray jArray = new JSONArray(str);

               for( int i=0;  i <jArray.length(); i++) {
                   json = jArray.getJSONObject(i);

               }


            } catch ( JSONException e) {
                e.printStackTrace();
            }

            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result)
        {
            try {
                textview.setText(json.getString("survey_textresponse"));

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //Hiding progress bar after done loading TextView.
            progressbar.setVisibility(View.GONE);

        }
    }


}
受保护的Void doInBackground(Void…arg0)
{
HttpClient myClient=新的默认HttpClient();
HttpPost myConnection=新的HttpPost(“http://192.168.1.5/send-data.php");
试一试{
response=myClient.execute(myConnection);
str=EntityUtils.toString(response.getEntity(),“UTF-8”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JSONArray jArray=新JSONArray(str);

对于(int i=0;i,因为您只在
json
引用中存储
JSONobject
的最后一个值

// assume JArray length = 5
for( int i=0;  i <jArray.length(); i++) {
    json = jArray.getJSONObject(i); 
    // only have last object value , in last iteration jArray.getJSONObject(4)
}

因为您只在
json
reference中存储
JSONobject
的最后一个值

// assume JArray length = 5
for( int i=0;  i <jArray.length(); i++) {
    json = jArray.getJSONObject(i); 
    // only have last object value , in last iteration jArray.getJSONObject(4)
}

完成后,
json
变量只包含
JsonArray
的最后一个元素。从JsonArray检索值时,可以在TextView上显示

for( int i=0;  i <jArray.length(); i++) {
      json = jArray.getJSONObject(i);
      textview.setText(textview.getText() + json.getString("survey_textresponse") + "\n");
}

for(int i=0;i您的
json
变量完成后仅包含
JsonArray
的最后一个元素。您可以在从JsonArray检索值时在TextView上显示

for( int i=0;  i <jArray.length(); i++) {
      json = jArray.getJSONObject(i);
      textview.setText(textview.getText() + json.getString("survey_textresponse") + "\n");
}

for(int i=0;i您为从JSONArray检索值而编写的循环不正确。它仅在循环中迭代时存储最后一个对象。您可以将检索到的值存储在字符串集合中。
下面是您可以遵循的示例代码:

private class RetrieveTask extends AsyncTask<Void,Void,Void>{
    List<String> jsonResults;
    String str;
    JSONObject json;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        jsonResults=new ArrayList<>();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        for(String data:jsonResults)
            textview.append(data);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://192.168.1.5/send-data.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try{
            JSONArray jArray = new JSONArray(str);

            for( int i=0;  i <jArray.length(); i++) {
                json = jArray.getJSONObject(i);
                if(json.optString("survey_textresponse")!=null){
                    jsonResults.add(json.getString("survey_textresponse"));
                }
            }


        } catch ( JSONException e) {
            e.printStackTrace();
        }

        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
私有类RetrieveTask扩展了AsyncTask{
列出结果;
字符串str;
JSONObject json;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
jsonResults=newarraylist();
}
@凌驾
受保护的void onPostExecute(void避免){
for(字符串数据:jsonResults)
textview.append(数据);
}
@凌驾
受保护的空位背景(空位…空位){
HttpClient myClient=新的默认HttpClient();
HttpPost myConnection=新的HttpPost(“http://192.168.1.5/send-data.php");
试一试{
response=myClient.execute(myConnection);
str=EntityUtils.toString(response.getEntity(),“UTF-8”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JSONArray jArray=新JSONArray(str);

对于(int i=0;i而言,您为从JSONArray检索值而编写的循环不正确。它仅在循环中迭代时存储最后一个对象。您可以将检索到的值存储在字符串集合中。 下面是您可以遵循的示例代码:

private class RetrieveTask extends AsyncTask<Void,Void,Void>{
    List<String> jsonResults;
    String str;
    JSONObject json;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        jsonResults=new ArrayList<>();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        for(String data:jsonResults)
            textview.append(data);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://192.168.1.5/send-data.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try{
            JSONArray jArray = new JSONArray(str);

            for( int i=0;  i <jArray.length(); i++) {
                json = jArray.getJSONObject(i);
                if(json.optString("survey_textresponse")!=null){
                    jsonResults.add(json.getString("survey_textresponse"));
                }
            }


        } catch ( JSONException e) {
            e.printStackTrace();
        }

        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
私有类RetrieveTask扩展了AsyncTask{
列出结果;
字符串str;
JSONObject json;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
jsonResults=newarraylist();
}
@凌驾
受保护的void onPostExecute(void避免){
for(字符串数据:jsonResults)
textview.append(数据);
}
@凌驾
受保护的空位背景(空位…空位){
HttpClient myClient=新的默认HttpClient();
HttpPost myConnection=新的HttpPost(“http://192.168.1.5/send-data.php");
试一试{
response=myClient.execute(myConnection);
str=EntityUtils.toString(response.getEntity(),“UTF-8”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JSONArray jArray=新JSONArray(str);

对于(int i=0;i pavneet_singh先生,它只填充最后一个值,如何才能循环所有json对象?@KamranAshiq正如我在回答中所说的,你只是在循环,但你也必须在循环时收集数据,所以如果你仍然有ve问题然后发布您的json响应PavNeet_singh先生,它只是填充最后一个值,如何才能循环所有json对象?@KamranAshiq正如我在回答中所说,您只是循环,但您也必须在循环时收集数据,因此您可以使用类似于
sb.append
list
的解决方案来收集数据ct数据,如果您仍然存在问题,那么发布json响应您的代码将产生崩溃,您无法从
doInBackground
I添加的textview.setText(……在onPostExecute方法中)更新UI,并且它起了作用。您的代码将产生崩溃,您无法从
doInBackground
I添加的textview.setText更新UI(…..在onPostExecute方法中,它工作了..它现在工作..我添加了textview.setText(…..在onPostExecute方法中,它工作了..我刚刚用给定的代码在onPostExecute方法中添加了这一行,它神奇地开始工作了,textview现在在it中显示所有行数据您确定它使用setText(..)发布所有行吗因为如果你循环列表中的值,那么它可能会覆盖TextView中的文本,而不是通过setText(..)附加它。不,先生,它还可以工作,没有重复的值现在可以工作了..我添加了TextView.setText(…..在onPostExecute方法中,它工作了..我刚刚用给定的代码在onPostExecute方法中添加了这一行,它神奇地开始工作,textview现在在Ita中显示所有行数据。您确定它使用setText(…)发布所有行吗因为如果您在列表中循环值,那么它可能会覆盖TextView中的文本,而不是通过setText(..)追加它。不,sir工作正常,没有重复的值