我有一个简单的Android演示,我已经放了一个editText和一个按钮,我想要的是当一个人进入anky城市名称并且当点击搜索按钮时。应该列出相关的WOEID列表,我有尝试如下,但它没有工作,它给了我一个例外,我的代码如下:
search.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;
}
}
logcat的
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)
请帮我解决,谢谢你提前........!
答案 0 :(得分:0)
您正在主线程上调用HTTP连接。你不应该这样做,因为如果连接滞后你的应用程序可能处于ANR状态并被操作系统终止。 你应该在另一个线程中移动Http连接的东西,例如你可以创建一个扩展AsyncTask的类,并在doBackground中实现你的逻辑。 希望这能帮到你。
PS:在我的博客中,我为你做了一个例子。如果您有兴趣,请查看here