我正在使用AsyncTask来调用yahooweather API。以下是代码:
public class myactivity 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 input_city;
EditText input_zip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
Button btn_findout = (Button) findViewById(R.id.btn_findout);
input_city = (EditText) findViewById(R.id.input_cityOrcountry);
input_zip = (EditText) findViewById(R.id.input_zip);
// when Zip textbox has focus
input_zip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String string = "";
input_city.setText(string);
}
}
});
// when city/country textbox has focus
input_city.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String string = "";
input_zip.setText(string);
}
}
});
// when findout button is clicked
btn_findout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String uriPlace = Uri.encode(input_city.getText().toString());
yahooAPIsQuery = yahooapisBase + "%22" + uriPlace + "%22"
+ yahooapisFormat;
Toast.makeText(getBaseContext(),
"Before entering into sub thread", Toast.LENGTH_LONG)
.show();
new WeatherAPITask().execute(yahooAPIsQuery);
Toast.makeText(getBaseContext(), "After sub thread",
Toast.LENGTH_LONG).show();
Log.i("my label", "back in main thread...");
// String woeidString = QueryYahooWeather(yahooAPIsQuery);
// input_city.setText(woeidString);
}
});
}
private String QueryYahooWeather(String queryString) {
String qResult = "";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);
try {
Log.i("WeatherApp", "digging into try block...");
Log.i("queryString", queryString);
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();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
Toast.makeText(getBaseContext(), "Returning from function",
Toast.LENGTH_LONG).show();
return qResult;
}
private class WeatherAPITask extends AsyncTask {
protected void onPostExecute(String result) {
Log.i("my label", "entering in onPostExecute");
Log.i("result", result);
input_city.setText("result");
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try {
Log.i("my label", "entering in doInBackground");
Log.i("params[0]", params[0].toString());
return QueryYahooWeather(params[0].toString());
} catch (Exception e) {
Log.i("my label", e.toString());
return null;
}
}
}
EditText input_city;
EditText input_zip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
Button btn_findout = (Button) findViewById(R.id.btn_findout);
input_city = (EditText) findViewById(R.id.input_cityOrcountry);
input_zip = (EditText) findViewById(R.id.input_zip);
// when Zip textbox has focus
input_zip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String string = "";
input_city.setText(string);
}
}
});
// when city/country textbox has focus
input_city.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String string = "";
input_zip.setText(string);
}
}
});
// when findout button is clicked
btn_findout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String uriPlace = Uri.encode(input_city.getText().toString());
yahooAPIsQuery = yahooapisBase + "%22" + uriPlace + "%22"
+ yahooapisFormat;
Toast.makeText(getBaseContext(),
"Before entering into sub thread", Toast.LENGTH_LONG)
.show();
new WeatherAPITask().execute(yahooAPIsQuery);
Toast.makeText(getBaseContext(), "After sub thread",
Toast.LENGTH_LONG).show();
Log.i("my label", "back in main thread...");
// String woeidString = QueryYahooWeather(yahooAPIsQuery);
// input_city.setText(woeidString);
}
});
}
private String QueryYahooWeather(String queryString) {
String qResult = "";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);
try {
Log.i("WeatherApp", "digging into try block...");
Log.i("queryString", queryString);
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();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
Toast.makeText(getBaseContext(), "Returning from function",
Toast.LENGTH_LONG).show();
return qResult;
}
private class WeatherAPITask extends AsyncTask {
protected void onPostExecute(String result) {
Log.i("my label", "entering in onPostExecute");
Log.i("result", result);
input_city.setText("result");
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try {
Log.i("my label", "entering in doInBackground");
Log.i("params[0]", params[0].toString());
return QueryYahooWeather(params[0].toString());
} catch (Exception e) {
Log.i("my label", e.toString());
return null;
}
}
}
调试代码后,我发现yahooAPI调用成功了,我可以在}
函数中看到XML响应。但是,只要执行此函数,就会抛出异常:
QueryYahooWeather
请帮帮我。
答案 0 :(得分:2)
从QueryYahooWeather
方法中删除所有Toast,因为此方法是从doInBackground(Object... params)
的{{1}}调用的,并且您无法从后台线程访问Uast元素,如Toast(也是一个Ui元素)。 / p>
注意:如果您想知道后台发生了什么,请使用Log代替Toast's
编辑:
将doInBackground更改为:
AsyncTask
答案 1 :(得分:1)
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
在QueryYahooWeather
中调用的doInBackground
中的。
您无法从后台主题调用UI
来电。