如何从url获取xml数据流而不仅仅是文件

时间:2015-03-03 18:36:30

标签: java android parsing url xmlpullparser

package org.me.myandroidstuff;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.me.myandroidstuff.CarParkClass;
import org.me.myandroidstuff.XMLPullParserHandler;

public class CarParkListingTestActivity extends Activity 
{

ListView listView;

private String url = "example.com?type=xml";

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.list);

    List<CarParkClass> carparks = null;
    try{
        XMLPullParserHandler parser = new XMLPullParserHandler();
        carparks = parser.parse(getAssets().open("carparks.xml"));
        //InputStream is = new FileInputStream(url);
        //carparks = parser.parse(is);
        ArrayAdapter<CarParkClass> adapter = 
                new ArrayAdapter<CarParkClass>(this,R.layout.list_item, carparks);
        listView.setAdapter(adapter);
    } catch(IOException e){
        e.printStackTrace();
    }

    listView.setBackgroundColor(Color.WHITE);

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    //getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

我想解析网址中的数据,而不是资产文件夹中的文件。我该如何接近这一步呢。

我尝试从我的资源文件夹解析,并且该应用程序运行良好,但是当我尝试从URL中解析时,它不起作用。

非常感谢您的帮助 感谢

1 个答案:

答案 0 :(得分:0)

您可以发出异步请求

如果您需要,也可以使用post方法:

public class MyAsyncRequest extends AsyncTask<String, String, String> {
        OnMyListener myListener;

        Context dContext;

        public interface OnMyListener {
            public void OnListener(String account);
        }

        public MyAsyncRequest(Context ctxt) {
            // TODO Auto-generated constructor stub
            this.dContext = ctxt;
            myListener = (OnMyListener) ctxt;
        }

        private ProgressDialog pDialog;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(dContext);
            pDialog.setMessage("Downloading..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.setCanceledOnTouchOutside(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String data = postData();
            return data;
        }

        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            pDialog.dismiss();
            myListener.OnListener(result);
        }

        public String postData(){

            ByteArrayOutputStream buf = new ByteArrayOutputStream();

            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
            HttpResponse response;
            try {

                //Http
                HttpGet getRequest = new HttpGet("https:/your.url");

                response = client.execute(getRequest);

                /*Checking response */
                if(response!=null){
                    InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    BufferedInputStream bis = new BufferedInputStream(in);

                    int result = bis.read();
                    while(result != -1) {
                        byte b = (byte)result;
                        buf.write(b);
                        result = bis.read();
                    }
                    //return buf.toString();
                }else{
                }

            } catch(Exception e) {
                e.printStackTrace();
            }

            return buf.toString();
        }
    }