请告知为什么如果我尝试获取数组的长度,我的小应用程序总是会出错。
请在下面找到我的代码:
1. 字符串解析器
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. 下载数据
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});
}
}
如何获得输出数组的长度??
textView.setText(output.length);