有人可以告诉我为什么这会导致我的应用程序崩溃吗?相同的代码适用于我的其他应用程序,这就是我如此困惑的原因。
我要做的是告诉我的应用程序转到http://omarcomputerservices.com/cartoons/cartoons.php?getCartoon&ID=1,然后将解析后的字符串从“*”字符中拆分出来。然后将其添加到数组中。
我已经添加了互联网权限。
package com.omarreyes.youtubechannels;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class StartingPoint extends Activity
{
ArrayList<String> images = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
Button get = (Button) findViewById(R.id.button1);
get.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
public void onClick(View v)
{
getImages();
}
};
private void getImages()
{
try
{
// Create a URL object
URL url = new URL("http://omarcomputerservices.com/cartoons/cartoons.php?getCartoon&ID=1");
// Read all of the text returned by the HTTP server
BufferedReader in = new BufferedReader
(new InputStreamReader(url.openStream()));
String htmlText;
while ((htmlText = in.readLine()) != null)
{
// Keep in mind that readLine() strips the newline characters
String[] data = htmlText.split("\\*");
images.add(data[0].toString());
}
in.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}