views:

55

answers:

3

I was given an assignment to develop a very simple weather app in Android using the data given from

http://forecast.weather.gov/zipcity.php

This would be my first actual android app, my only other experience is watching/reading many tutorials on getting started.

To give some background information of the app, it simply needs to have one activity that is a text field for the user to enter a city, state combination, or just a zip code. This then needs to be submitted to the website's (listed above) form.

After this has been submitted, my app needs to retrieve page sent in response and display it in a simple layout (possibly a listView of the days of the week).

My first question is, how would I go about submitting a text input to the above website's form? Would I have to know the name of the form's field to submit to it precisely?

In general, how should I start this process?


Edited version:

Using this code:

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://forecast.weather.gov/zipcity.php?inputstring=04419");

    HttpResponse httpResp;

    try {
        httpResp = httpClient.execute(post);
        StatusLine status = httpResp.getStatusLine();
        Toast.makeText(getApplicationContext(), status.getStatusCode(), Toast.LENGTH_SHORT).show();


    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

I'm getting a:

10-26 15:29:56.686: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol

error in the debug view. Is this error related? Is my use of toast reasonable for testing if this http interaction is successful?

A: 

When I checked out your URL, I entered city and state.This is the URL it generates in HTTP POST :

http://forecast.weather.gov/MapClick.php?CityName=Edison&state=NJ&site=PHI&textField1=40.5288&textField2=-74.3693

So your will do a HTTP POST with the CityName and state URL parameters. It will be something like this if you use apache HttpClient in your android program : ( It should be OK to use HTTP GET too I guess)


        HttpPost post = new HttpPost(url);//construct url with cityName and State 
        HttpResponse httpResp = httpClient.execute(post);
       StatusLine status = httpResp.getStatusLine();


Hope this helps.

Ramp
This looks to be like an easy approach, but what about zip code entries?
PetrakovichJ
Digging through the HTML source, it looks like you just need to provide city,state or ZIP code through the URL: "http://forecast.weather.gov/zipcity.php?inputstring=08802 " (or) "http://forecast.weather.gov/zipcity.php?inputstring=Matawan,nj"
Ramp
PetrakovichJ, you may want to accept or give credit to the answers you like. Most of the answers here gives u all the information you need.
Ramp
A: 

When you want to see what a post request in passing use Firefox + tamperdata, that way you can look at how to use the webservice.

I took a look at it, I searched for "33129" and when you enter text on the left hand box to start a search it simply pases 2 parameters:

inputstring = "33129"
Go2 = "Go"

That would be one way to do it, on the other hand, once the request is finished it transforms it into another request, thats more specific. You can search by city state or zip, not both.

If you request with a zip you get redirected to:

http://forecast.weather.gov/MapClick.php?CityName=Miami&state=FL&site=MFL&lat=25.77&lon=-80.2

So there is probably a redirection going on there.

You are going to have to take a close look at how to work with this.

Now, to do a post request in android use a name value pair like this.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("inputstring","33129));

Ramp's answer shows the rest.

Also, ALL comunication should be done on a thread that is not the main UI thread or you will get a ANR error.

blindstuff
A: 

Something like this:

        HttpClient hc = new DefaultHttpClient();
        List<NameValuePair> l = new ArrayList<NameValuePair>(6);
        l.add(new BasicNameValuePair("fieldname", "fieldvalue"));
             //More fields...
             HttpPost pr = new HttpPost(TheURL);
        pr.setEntity(new UrlEncodedFormEntity(l, "the-proper-encoding"));
        HttpResponse Resp = hc.execute(pr);
        if(Resp.getStatusLine().getStatusCode() != 200)
                 //Fail...
Seva Alekseyev