我的应用程序意外崩溃,我不明白为什么。我正在使用HttpURLConnection来检索一个 json 文件,并且我正在尝试使用JsonReader类来读取并使用该文件。问题是应用程序在读取或对 JsonReader 实例执行任何操作后崩溃。
我已经用扫描仪读取了输入流,但这会导致类似的问题。但是,当使用扫描仪阅读时,我可以从服务器打印少量 json。因此,来自服务器的数据也可能会进入 JsonReader。
这是我的代码:
public class ConnectToServer implements Runnable {
private URL connectionUrl;
private MainActivity output;
public ConnectToServer(URL url, MainActivity mainActivity) {
connectionUrl = url;
output = mainActivity;
}
public void printError(String errorMessage){
output.output(errorMessage);
}
public InputStream GetInputStream(){
try {
HttpURLConnection connection = (HttpURLConnection)connectionUrl.openConnection();
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
if (inputStream == null){
printError("Input stream is null");
}
return inputStream;
} catch (IOException ex){
printError("IO exception has been thrown");
} catch (Exception ex){
printError("Normal exception thrown: " + ex.getClass().getSimpleName());
}
return null;
}
@Override
public void run() {
InputStream inputStream = GetInputStream();
try {
if (inputStream == null){
printError("Input stream not initiated");
} else {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
printError(reader.toString());
reader.close();
}
} catch (Exception ex){
printError("Exception while printing input stream: " + ex.getClass().getSimpleName());
}
printError("Thread finished");
}
这将在主 UI 线程中创建的线程上运行。
public void connect(){
output("connect started");
boolean success = true;
try {
URL url = new URL("http://geonews.azurewebsites.net/api/Location");
serverConnection = new ConnectToServer(url,this);
Thread thread = new Thread(serverConnection);
thread.start();
} catch (MalformedURLException ex){
output("Messed up the URL");
success = false;
}
if (success){
output("Thread has been started");
} else {
output("exception was thrown while trying to run thread");
}
}
有谁知道为什么这段代码会导致我的应用程序崩溃?即使它到达“线程完成”,它也会很快崩溃。
顺便说一句,我意识到我应该使用 AsyncTask,但我已经走上了这条路,我宁愿先走这条路。
日志猫:
06-14 22:45:00.058 4032-4052/com.tomsapps.thomas.jsonreadertestapp E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-314
Process: com.tomsapps.thomas.jsonreadertestapp, PID: 4032
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:867)
at android.view.View.requestLayout(View.java:17364)
at android.view.View.requestLayout(View.java:17364)
at android.view.View.requestLayout(View.java:17364)
at android.view.View.requestLayout(View.java:17364)
at android.view.View.requestLayout(View.java:17364)
at android.view.View.requestLayout(View.java:17364)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
at android.view.View.requestLayout(View.java:17364)
at android.widget.TextView.checkForResize(TextView.java:6798)
at android.widget.TextView.updateAfterEdit(TextView.java:7693)
at android.widget.TextView.handleTextChanged(TextView.java:7709)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9440)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:272)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:33)
at android.widget.TextView.append(TextView.java:3616)
at android.widget.TextView.append(TextView.java:3603)
at com.tomsapps.thomas.jsonreadertestapp.MainActivity.output(MainActivity.java:63)
at com.tomsapps.thomas.jsonreadertestapp.ConnectToServer.printError(ConnectToServer.java:26)
at com.tomsapps.thomas.jsonreadertestapp.ConnectToServer.run(ConnectToServer.java:74)
at java.lang.Thread.run(Thread.java:818)