从Web服务解析日出和日落值 [英] Parsing sunrise and sunset values from web service

查看:139
本文介绍了从Web服务解析日出和日落值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,看来我坚持,并可能与一些帮助做的。

Okay, so it appears I'm stuck and could do with some help.

在成功调用与此WebService 我当前经纬度我得到了整个XML文件重新进行回应。

After successfully calling this webservice with my current longitude and latitude I am getting the entire xml file back in response.

但是xml文件我想的唯一位是日出和日落时间,我怎么去分析这些,只有到这些TextViews?

However the only bit of the xml file I want is the sunrise and sunset times, how do I go about parsing these and only these into TextViews?

我已经看了,并试图在XmlPullParser,和的DOMParser无数SAXParser的教程,现在我挣扎。

I've looked at and attempted countless tutorials on XmlPullParser, DOMParser and SAXParser and I'm now struggling.

code的要求:

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SunriseSunset extends Activity implements OnClickListener {

    public Button getLocation;
    public TextView LongCoord;
    public TextView LatCoord;
    public double longitude;
    public double latitude;
    public LocationManager lm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sunrisesunset);
        findViewById(R.id.my_button).setOnClickListener(this);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
                new MyLocationListener());

        getLocation = (Button) findViewById(R.id.GetLocation);
        LongCoord = (TextView) findViewById(R.id.LongCoord);
        LatCoord = (TextView) findViewById(R.id.LatCoord);

        getLocation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // When GetLocation button is clicked the showCurrentLocation
                // function is ran
                showCurrentLocation();
            }

        });
    }

    protected void showCurrentLocation() {
        // TODO Auto-generated method stub
        // This is called to find current location based on GPS data and sends
        // this to LongCoord and LatCoord TextViewsw
        Location location = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitude = location.getLatitude();
        longitude = location.getLongitude();

        LongCoord.setText(Double.toString(longitude));
        LatCoord.setText(Double.toString(latitude));
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button) findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask<Void, Void, String> {

        protected String getASCIIContentFromEntity(HttpEntity entity)
                throws IllegalStateException, IOException {
            InputStream in = entity.getContent();
            StringBuffer out = new StringBuffer();
            int n = 1;
            while (n > 0) {
                byte[] b = new byte[4096];
                n = in.read(b);
                if (n > 0)
                    out.append(new String(b, 0, n));
            }
            return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            //Finds todays date and adds that into the URL
            Calendar c = Calendar.getInstance();
            System.out.println("Current time => " + c.getTime());
            SimpleDateFormat df = new SimpleDateFormat("dd/MM");
            String formattedDate = df.format(c.getTime());

            String finalURL = "http://www.earthtools.org/sun/"
                    + LatCoord.getText().toString().trim() + "/"
                    + LongCoord.getText().toString().trim() + "/"
                    + formattedDate + "/99/0";
            HttpGet httpGet = new HttpGet(finalURL);
            String text = null;

            try {
                HttpResponse response = httpClient.execute(httpGet,
                        localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } catch (Exception e) {
                return e.getLocalizedMessage();
            }
            return text;
        }

        protected void onPostExecute(String results) {
            if (results != null) {
                EditText et = (EditText) findViewById(R.id.my_edit);
                et.setText(results);
            }
            Button b = (Button) findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }

    class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }
}

在这里,正在使用的布局:

And here is the layout being used:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SunriseSunset" >

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Date:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

       <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Today"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Location:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Current Location"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <Button
        android:id="@+id/GetLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Find Location" />

    <TextView
        android:id="@+id/LatCoord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/LongCoord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Calculate Sunrise/Sunset Time"
        android:id="@+id/my_button"/>
    <EditText 
        android:layout_margin="20dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="30"
        android:maxLines="30"
        android:textSize="12sp"
        android:editable="false"
        android:id="@+id/my_edit"/>

</LinearLayout>

在此先感谢您的帮助!

Thanks in advance for your help!

推荐答案

您可以尝试用以下替换您的onPostExecute并添加此导入...

You can try replacing your onPostExecute with the one below and add this import...

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


protected void onPostExecute(String results) {
        if (results != null) {
        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource s = new InputSource(new StringReader(result));
            Document doc = dBuilder.parse(s);
            doc.getDocumentElement().normalize();
                            //EditText et = (EditText) findViewById(R.id.my_edit);
                            TextView tv = (TextView) findViewById(R.id.Date);
                            tv.setText("sunrise at "+doc.getElementsByTagName("sunrise").item(0).getTextContent() + " sunset at "+doc.getElementsByTagName("sunset").item(0).getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Button b = (Button) findViewById(R.id.my_button);
    b.setClickable(true);
    }

编辑:我不知道要设置哪些TextView的,但我想这一个(R.id.Date)。
您可以更改ID,如果这不是正确的。

I'm not sure which TextView you want to set but I guess this one (R.id.Date). You can change the id if it's not the right one.

//EditText et = (EditText) findViewById(R.id.my_edit); 
TextView tv = (TextView) findViewById(R.id.Date);
tv.setText...

这篇关于从Web服务解析日出和日落值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆