I have this code and I dont know what is the error: package com.shoukurasou;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
public class ShoutActivity extends Activity {
public void update_shouts() throws IOException{
URL url;
String inputLine = null;
url = new URL("http://MYSITE/shouts.php");
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "Windows-1252"));
inputLine = br.readLine().toString();
List<String> shouts = new ArrayList<String>();
String[] shoutsplit = inputLine.split("<item>");
for(int i = 0; i < shoutsplit.length; i++){
String username = shoutsplit[i].split("<info>")[0].toString();
String shout = shoutsplit[i].split("<info>")[1].toString();
shouts.add(username+": "+Html.fromHtml(shout));
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shouts);
final ListView list = (ListView) findViewById(R.id.list);
if(list != null){
ShoutActivity.this.runOnUiThread(new Runnable() {
public void run() {
list.setAdapter(null);
list.setAdapter(adapter);
}
});
}else{
new AlertDialog.Builder(this).setTitle("Erro!").setMessage("Este erro é comum, por favor, feche e abra o aplicativo novamente.").setNeutralButton("Ok", null).show();
}
br.close();
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shout);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
update_shouts();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, 1000);
}
public void sendShout(View v) throws IOException{
String login = null;
String password = null;
String edit1value = null;
Bundle extras = getIntent().getExtras();
login = extras.getString("login").toString();
password = extras.getString("password").toString();
EditText edit1 = (EditText) findViewById(R.id.editText1);
edit1value = URLEncoder.encode(edit1.getText().toString());
URL shout;
shout = new URL("http://MYSITE/send_shout.php?username="+login+"&password="+password+"&shout="+edit1value);
URLConnection connection_shout = shout.openConnection();
BufferedReader brshout = new BufferedReader(new InputStreamReader(connection_shout.getInputStream()));
edit1.setText("");
}
}
Error:
11-11 01:34:34.649: E/AndroidRuntime(272): FATAL EXCEPTION: Timer-0 11-11 01:34:34.649: E/AndroidRuntime(272): java.lang.ArrayIndexOutOfBoundsException 11-11 01:34:34.649: E/AndroidRuntime(272): at com.shoukurasou.ShoutActivity.update_shouts(ShoutActivity.java:40) 11-11 01:34:34.649: E/AndroidRuntime(272): at com.shoukurasou.ShoutActivity$2.run(ShoutActivity.java:76) 11-11 01:34:34.649: E/AndroidRuntime(272): at java.util.Timer$TimerImpl.run(Timer.java:289)
I dont know what is the error, and I'm sorry about my english, I'm from brazil, thx