我有一个简单的android演示,我放了一个editText和一个按钮,我想要的是当一个输入anky城市名称并单击搜索按钮时。应该列出相关WOEID的列表,我尝试如下但它不起作用,它给了我一个例外,我的代码如下:
搜索.java
public class SesachActivity extends Activity {
final String yahooapisBase = "http://query.yahooapis.com/v1/public/yql?q=select*from%20geo.places%20where%20text=";
final String yahooapisFormat = "&format=xml";
String yahooAPIsQuery;
EditText place;
Button search;
ListView listviewWOEID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sesach);
place = (EditText) findViewById(R.id.place);
search = (Button) findViewById(R.id.search);
listviewWOEID = (ListView) findViewById(R.id.woeidlist);
search.setOnClickListener(searchOnClickListener);
}
Button.OnClickListener searchOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
if (place.getText().toString().equals("")) {
Toast.makeText(getBaseContext(), "Enter place!",
Toast.LENGTH_LONG).show();
} else {
ArrayList<String> l = QueryYahooAPIs();
ArrayAdapter<String> aa = new ArrayAdapter<String>(
getBaseContext(), android.R.layout.simple_list_item_1,
l);
listviewWOEID.setAdapter(aa);
}
}
};
private ArrayList<String> QueryYahooAPIs() {
String uriPlace = Uri.encode(place.getText().toString());
yahooAPIsQuery = yahooapisBase + "%22" + uriPlace + "%22"
+ yahooapisFormat;
String woeidString = QueryYahooWeather(yahooAPIsQuery);
Document woeidDoc = convertStringToDocument(woeidString);
/*System.out.println(":::::::::::WOEID DOC::::::::::::"+woeid);*/
return parseWOEID(woeidDoc);
}
private ArrayList<String> parseWOEID(Document srcDoc) {
ArrayList<String> listWOEID = new ArrayList<String>();
NodeList nodeListDescription = srcDoc.getElementsByTagName("woeid");
if (nodeListDescription.getLength() >= 0) {
for (int i = 0; i < nodeListDescription.getLength(); i++) {
listWOEID.add(nodeListDescription.item(i).getTextContent());
}
} else {
listWOEID.clear();
}
return listWOEID;
}
private Document convertStringToDocument(String src) {
Document dest = null;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = dbFactory.newDocumentBuilder();
dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
.show();
} catch (SAXException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
return dest;
}
private String QueryYahooWeather(String queryString) {
String qResult = "";
HttpClient httpClient = new DefaultHttpClient();
// return Uri.encode(queryString);
HttpGet httpGet = new HttpGet(queryString);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null) {
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
return qResult;
}
}
日志猫
01-02 07:00:34.624: E/AndroidRuntime(1792): FATAL EXCEPTION: main
01-02 07:00:34.624: E/AndroidRuntime(1792): android.os.NetworkOnMainThreadException
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-02 07:00:34.624: E/AndroidRuntime(1792): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-02 07:00:34.624: E/AndroidRuntime(1792): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-02 07:00:34.624: E/AndroidRuntime(1792): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-02 07:00:34.624: E/AndroidRuntime(1792): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.example.dayweather.SesachActivity.QueryYahooWeather(SesachActivity.java:144)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.example.dayweather.SesachActivity.QueryYahooAPIs(SesachActivity.java:81)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.example.dayweather.SesachActivity.access$0(SesachActivity.java:74)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.example.dayweather.SesachActivity$1.onClick(SesachActivity.java:62)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.view.View.performClick(View.java:4240)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.view.View$PerformClick.run(View.java:17721)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.os.Handler.handleCallback(Handler.java:730)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.os.Looper.loop(Looper.java:137)
01-02 07:00:34.624: E/AndroidRuntime(1792): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 07:00:34.624: E/AndroidRuntime(1792): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 07:00:34.624: E/AndroidRuntime(1792): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 07:00:34.624: E/AndroidRuntime(1792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 07:00:34.624: E/AndroidRuntime(1792): at dalvik.system.NativeStart.main(Native Method)
请帮我解决它,提前谢谢你........!