package com.nicotera.colton.londontransitguide;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.*;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class RoutesActivity extends Activity implements OnItemSelectedListener {
private static final String TAG = "RoutesActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routes);
Spinner spinner = (Spinner) findViewById(R.id.route_name_spinner); // Create an ArrayAdapter using the string array and a default spinner layout
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.routes_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner
spinner.setAdapter(adapter);
Log.i(TAG, "spinner populated");
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Log.i(TAG, "Item selected");
int tempPos = pos;
Log.i(TAG, ("Position of selected item: " + tempPos));
int routeSelected;
if (tempPos < 17)
routeSelected = (tempPos + 1);
else if (tempPos >= 17 && tempPos < 29)
routeSelected = (tempPos + 2);
else
routeSelected = (tempPos + 3);
String temp;
if (routeSelected < 10)
temp = ("0") + routeSelected;
else
temp = ("") + routeSelected;
String url = "http://www.ltconline.ca/WebWatch/MobileAda.aspx?r=" + temp;
try {
urlParse(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void urlParse (String url) throws IOException {
Log.i(TAG, "Tried to parse url");
String [] directions = new String [3];
String [] directionNames = new String [3];
Pattern routeDirPattern = Pattern.compile("\\&d=(\\d{1,2})");
Connection conn = Jsoup.connect(url);
/*LINE 82 */ Document doc = conn.get();
int i = 0;
Elements routeLinks = doc.select("a[href]");
for (Element routeLink : routeLinks) {
i = (i + 1);
String name = routeLink.text();
Attributes attrs = routeLink.attributes();
String href = attrs.get("href");
Matcher m = routeDirPattern.matcher(href);
if (m.find()) {
String number = m.group(1);
directions [i] = number;
directionNames [i] = name;
Log.i(TAG, directionNames [i]);
}
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
为长代码道歉,但我想我应该发布所有这些,所以我不必稍后发布。发生的事情是urlParse方法不起作用,特别是在第82行(不完全是第82行,因为我在发布之前删除了一些注释)。有人看到问题是什么吗?
LogCat发布于:
12-08 20:39:38.384: I/RoutesActivity(765): Item selected
12-08 20:39:38.384: I/RoutesActivity(765): Position of selected item: 0
12-08 20:39:38.394: I/RoutesActivity(765): Tried to parse url
12-08 20:39:38.454: D/AndroidRuntime(765): Shutting down VM
12-08 20:39:38.484: W/dalvikvm(765): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-08 20:39:38.505: E/AndroidRuntime(765): FATAL EXCEPTION: main
12-08 20:39:38.505: E/AndroidRuntime(765): android.os.NetworkOnMainThreadException
12-08 20:39:38.505: E/AndroidRuntime(765): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-08 20:39:38.505: E/AndroidRuntime(765): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-08 20:39:38.505: E/AndroidRuntime(765): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-08 20:39:38.505: E/AndroidRuntime(765): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-08 20:39:38.505: E/AndroidRuntime(765): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
12-08 20:39:38.505: E/AndroidRuntime(765): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:408)
12-08 20:39:38.505: E/AndroidRuntime(765): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
12-08 20:39:38.505: E/AndroidRuntime(765): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
12-08 20:39:38.505: E/AndroidRuntime(765): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:148)
12-08 20:39:38.505: E/AndroidRuntime(765): at com.nicotera.colton.londontransitguide.RoutesActivity.urlParse(RoutesActivity.java:82)
12-08 20:39:38.505: E/AndroidRuntime(765): at com.nicotera.colton.londontransitguide.RoutesActivity.onItemSelected(RoutesActivity.java:62)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.widget.AdapterView.access$200(AdapterView.java:49)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.os.Handler.handleCallback(Handler.java:725)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.os.Looper.loop(Looper.java:137)
12-08 20:39:38.505: E/AndroidRuntime(765): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-08 20:39:38.505: E/AndroidRuntime(765): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 20:39:38.505: E/AndroidRuntime(765): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 20:39:38.505: E/AndroidRuntime(765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 20:39:38.505: E/AndroidRuntime(765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 20:39:38.505: E/AndroidRuntime(765): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
这个问题几乎每两天就出现在Stack Overflow上。 这是因为你无法从主线程触摸网络。
见我的回答Unfortunately, Msger has stopped. FATAL Exception: main
在RoutesActivity中创建一个新类
public class RoutesActivity extends Activity{
........ blah blah
private class MyInnerClass extends AsyncTask<String, Void, String> {
String [] directions = new String [3];
String [] directionNames = new String [3];
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try{
Pattern routeDirPattern = Pattern.compile("\\&d=(\\d{1,2})");
Connection conn = Jsoup.connect(params[0]);
Document doc = conn.get();
int i = 0;
Elements routeLinks = doc.select("a[href]");
for (Element routeLink : routeLinks) {
i = (i + 1);
String name = routeLink.text();
Attributes attrs = routeLink.attributes();
String href = attrs.get("href");
Matcher m = routeDirPattern.matcher(href);
if (m.find()) {
String number = m.group(1);
directions [i] = number;
directionNames [i] = name;
Log.i(TAG, directionNames [i]);
}
}
}catch(Exception e){Log.d("doinbackground exception", e.toString());}
return "Done";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// do whatever you wana do with directions[] and directionNames[] here
}
}
}
更改以下内容
try {
urlParse(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
到
new MyInnerClass().execute(url);