简体   繁体   中英

Programmatically filling in a website form using MultipartEntity

I'm taking a mobile development class, which focuses on Android, and for my term project I thought it would be cool if I made a little application that returns a list of cancer-related events and fundraisers. Basically what I have to do is programmatically fill in a webform given criteria that is input from my application, and parse the returned results to give a list of events, because for some reason the American Cancer Society doesn't keep a public list of all events. This is my first real experience with android, and I have almost zero experience with network programming. If I really wanted to, I could just change the URL I go to based on the paramaters given to me, because the ACS event search URLs all look almost exactly the same, but I want to do it "right". I looked at this post and this one for guidance, which led me to the MultipartEntity. They've been very helpful, but I really am not sure what to do next. Code is below:

    //Base case, creates entity based on Entered ZIP Code
    public void sendRequest()
    {
        EditText MyEditText = (EditText)findViewById(R.id.zip_edit_text);
        String ZIP = MyEditText.getText().toString();

        HttpClient defaultClient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.cancer.org/Involved/Participate/app/event-search");

        try{
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("ZIP",new StringBody(ZIP));
            httppost.setEntity(entity);
            HttpResponse response = defaultClient.execute(httppost);
            HttpEntity result = response.getEntity();

            InputStream stream = result.getContent();
            String s = new Scanner(stream).useDelimiter("\\A").next();

            Intent intent = new Intent(HomeScreen.this, ListResults.class);
            startActivity(intent);
            AlertDialog dialog = new AlertDialog.Builder(this).create();
            dialog.setMessage(s);
            dialog.show();

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

It's pretty bare-bones right now, as you can see. The AlertDialog is used just to see what the HttpResponse looks like, and it seems like it does the POST correctly, and the ZIP code ends up in the right text field, but it doesn't actually "click" the search button. Personally, I think either: 1.) My HttpPost object's URL was incorrect 2.) I used POST instead of GET, or i should POST then GET

I really have tried to work this out myself, and have searched StackOverflow, but I've really come to a rough patch, and as I said before, my network programming experience is near nonexistent. Any help would be appreciated.

I would suggest that you do a printout the URL that was sent through your multipart method, do a search via the web browser, and see if both URL matches. If the URL doesn't match, it means that there's something wrong while setting your entity, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM