밑의 소스에
searchXml(searhTxt);를
btn.setOnClickListener에 넣었을때는
리스트가 쫙 뿌리지면 서 잘 나왔었는데
ProgressDialog 를 로딩중에 뿌려줄려고
protected Void doInBackground(String... params) {
try{
// HttpGet httpget = new HttpGet();
// ResponseHandler <String> resposeHandler = new BasicResponseHandler();
// Content = Client.execute(httpget , resposeHandler);
System.out.println("================");
searchXml(searhTxt);
System.out.println("================");
}catch(Exception e){
Error = e.getMessage();
cancel(true);
}
return null;
}
이렇게 넣었더니
화면에 Only the original thread that create a view hierarchy can touch its views 라고 뜨며
안되네요 ..
밑은 로그입니다
답변 주시면 감사하겠습니다(__)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listNumber = 10;
Button btn = (Button) findViewById(R.id.searchBtn);
et = (EditText) findViewById(R.id.searchTxt);
tv = (TextView) findViewById(R.id.title);
listView = (ListView) this.getListView();
...
// 버튼 클릭시 xml을 읽기위한 함수 호출
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final String searhTxt = et.getText().toString();
if (searhTxt == "") {
} else {
new GrabURL(searhTxt).execute(searhTxt);
}
}
});
}
// XmlPullParser를 이용 xml을 읽는다.
public void searchXml(String searchTxt) {
String m_searchTxt = "";
try {
m_searchTxt = URLEncoder.encode(searchTxt);
} catch (Exception e1) {
e1.printStackTrace();
}
m_sConnectUrl = "http://openapi.naver.com/search?key=29b0a07ddfe7502eb2d0c585cd448a32&query="
+ m_searchTxt + "&target=image&start=1&display=" + listNumber;
m_xmlData = new ArrayList<XmlData>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL u = new URL(m_sConnectUrl);
InputStream in = u.openStream();
xpp.setInput(in, "utf-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
}
else if (eventType == XmlPullParser.END_DOCUMENT) {
}
else if (eventType == XmlPullParser.START_TAG) {
sTag = xpp.getName();
if (sTag.equals("title")) {
xmlData = new XmlData();
xmlData.d_title = xpp.nextText();
}
if (sTag.equals("link")) {
xmlData.d_link = xpp.nextText();
}
if (sTag.equals("thumbnail")) {
xmlData.d_thumbnail = xpp.nextText();
}
} else if (eventType == XmlPullParser.END_TAG) {
sTag = xpp.getName();
if (sTag.equals("item")) {
m_xmlData.add(xmlData);
xmlData = null;
}
} else if (eventType == XmlPullParser.TEXT) {
}
eventType = xpp.next();
}
} catch (Exception e) {
}
adapter = new XmlListAdapter(this, R.layout.row, m_xmlData);
setListAdapter(adapter);
}
public void listAdd() {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL u = new URL(m_sConnectUrl);
// InputStream in = u.openConnection().getInputStream();
InputStream in = u.openStream();
xpp.setInput(in, "utf-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
} else if (eventType == XmlPullParser.END_DOCUMENT) {
} else if (eventType == XmlPullParser.START_TAG) {
sTag = xpp.getName();
if (sTag.equals("title")) {
xmlData = new XmlData();
xmlData.d_title = xpp.nextText();
}
if (sTag.equals("link")) {
xmlData.d_link = xpp.nextText();
}
if (sTag.equals("thumbnail")) {
xmlData.d_thumbnail = xpp.nextText();
}
} else if (eventType == XmlPullParser.END_TAG) {
sTag = xpp.getName();
if (sTag.equals("item")) {
m_xmlData.add(xmlData);
xmlData = null;
}
} else if (eventType == XmlPullParser.TEXT) {
}
eventType = xpp.next();
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
}
}
...
...
private class GrabURL extends AsyncTask<String, Void, Void>{
// private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(XmlParser.this);
private String searhTxt ="";
private GrabURL(String _searhTxt){
this.searhTxt = _searhTxt;
}
/**
* 작업이 시작되기 직전에 화면에 처리해야할 작업
*/
protected void onPreExecute() {
super.onPreExecute();
Dialog.setMessage("로딩중");
Dialog.show();
}
/**
* 백그라운드에서 처리해야할 내용
*/
@Override
protected Void doInBackground(String... params) {
try{
// HttpGet httpget = new HttpGet();
// ResponseHandler <String> resposeHandler = new BasicResponseHandler();
// Content = Client.execute(httpget , resposeHandler);
System.out.println("================");
searchXml(searhTxt);
System.out.println("================");
}catch(Exception e){
Error = e.getMessage();
cancel(true);
}
return null;
}
/**
* 백그라운드 작업이 종료된 직후 화면에 처리해야 할 작업
*/
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Dialog.dismiss();
if(Error != null){
Toast.makeText(XmlParser.this, Error, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(XmlParser.this, "Source : "+Content , Toast.LENGTH_LONG).show();
}
}
}
}