简体   繁体   中英

Getting length of array in java

Please advice why if i try to get length of an array, my little application always getting error.

Please find my code below:

1. String Parser

    package dl.data.background;

    import java.util.ArrayList;
        public class StringParser {

        public StringParser()
        {

        }

        public ArrayList<Object> Parse (String input) {
            ArrayList<Object> output = new ArrayList <Object>();

            int pointer = 0;
            String data = "";

            while (pointer <= input.length()-1)
            {
                if(input.charAt(pointer) == '#'){
                    output.add(data);
                    data = "";
                    pointer++;
                }
                if(pointer < input.length())
                {
                    data += input.charAt(pointer);
                }
                pointer++;
            }
            return output;
        }
    } 

2. Downloading data

    package dl.data.background;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;

    public class BackgroundDataActivity extends Activity {
    private TextView textView;



    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.TextView01);

    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";
      for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse execute = client.execute(httpGet);
          InputStream content = execute.getEntity().getContent();

          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
          String s = "";
          while ((s = buffer.readLine()) != null) {
            response += s;
          }

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return response;
    }

    protected void onPostExecute(String result) {
        StringParser stringParser = new StringParser();
            ArrayList<Object> arrayList = stringParser.Parse(result);

            Object [] output = arrayList.toArray();

            // how to get length of output array?? 
        textView.setText(output.length);

      }
    }

    public void readWebpage(View view) {
      DownloadWebPageTask task = new DownloadWebPageTask();
      String url1 = "http://atmbersama.16mb.com/atmlocator.php?ct=SEL_Id&lat=-6.302161&lng=106.743679&radius=20";

      task.execute(new String[] {url1});
       }
     }

how to get length of output array??

textView.setText(output.length);

length is an Integer , and setText(CharSequence) expects a CharSequence :

textView.setText(String.valueOf(output.length));

The code compiles fine, because there is in fact an overloaded method setText(int resid) that actually takes an integer. However, this method will try to load a resource with the given id instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM