问题描述
- 对于这段图灵机器人调用代码,我不太理解,可以帮帮我么?
-
主类
enter code here
package zlexample.tuling;import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;public class MainActivity extends ActionBarActivity implements HttpGetDataListener {
private HttpData data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); data=(HttpData) new HttpData("http://www.tuling123.com/openapi/api?key=cc1dc647bdf0dc4bc7af64edbbda8346&info=+北京", this).execute(); } @Override public void getDataUrl(String data) { // TODO Auto-generated method stub System.out.println(data); //这里的data和上面new的data一样么(我感觉不一样的,因为httpData类中的listener的调用,给data赋值了么???)?????还有着这里重写这个方法干什么的?? //莫非是因为定义了接口就必须重写这个方法??接口实际的作用只是限制了data的数据类型?? }
}
另一个类httpDatapublic class HttpData extends AsyncTask{
private HttpClient httpClient; private HttpGet httpGet; private String url; private HttpResponse httpResponse; private HttpEntity httpEntity; private InputStream in; private HttpGetDataListener listener; public HttpData(String url,HttpGetDataListener listener) { // TODO Auto-generated constructor stub this.url=url; this.listener=listener; } @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub httpClient=new DefaultHttpClient(); httpGet=new HttpGet(url); try { httpResponse=httpClient.execute(httpGet); httpEntity=httpResponse.getEntity(); in=httpEntity.getContent(); BufferedReader buReader=new BufferedReader(new InputStreamReader(in)); String line=null; StringBuffer stringBuffer = new StringBuffer(); while ((line=buReader.readLine())!=null) { stringBuffer.append(line); } return stringBuffer.toString(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub listener.getDataUrl(result); super.onPostExecute(result); }
}
定义的一个接口
package zlexample.tuling;public interface HttpGetDataListener {
void getDataUrl(String data);
}
时间: 2024-11-17 04:34:06