Why is my response after posting data different in Android?
<p>Below is code I've tested out as an independent java file outside of Android, which works and returns a String custID for me and prints it in the console. However, when I add the file to use in my Android project, it doesn't return the same data.</p>

<p>My file posts data to a login and then gets the value of CustId from the URL (www.xxxxxx.com/x.aspx?CustId=12345) after the login has been verified. However, in the Android environment, doing the same thing just returns the URL of the original login page as if the login wasn't verified or something. </p>

<p>I'm thinking it has something to do with my post data?</p>

<pre><code>package podium.raceway;

import java.io.*;
import java.net.*;

import android.util.Log;

public class PodiumConnection
{
    private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
    private static final String TARGET_URL = "http://www.clubspeedtiming.com/prkapolei/login.aspx";

    public PodiumConnection(){
        Log.d("CUSTID", httpPostLogin("oipsl")); 
            //or System.out.println(httpPostLogin("oipsl"); in non-Android environment
    }
    /**
     * The single public method of this class that
     * 1. Prepares a login message
     * 2. Makes the HTTP POST to the target URL
     * 3. Reads and returns the response
     *
     * @throws IOException
     * Any problems while doing the above.
     *
     */
    public String httpPostLogin (String user)
    {
        String response = null;
        try
        {
            // Prepare the content to be written
            // throws UnsupportedEncodingException
            String urlEncodedContent = preparePostContent(user);            
            HttpURLConnection urlConnection = doHttpPost(TARGET_URL, urlEncodedContent);
            response = readResponse(urlConnection);
        }
        catch(IOException ioException)
        {
            System.out.println("Problems encountered.");
        }

        return response;
    }

    /**
     * Using the given username and password, and using the static string variables, prepare
     * the login message. Note that the username and password will encoded to the
     * UTF-8 standard.
     *
     * @param loginUserName
     * The user name for login
     *
     * @param loginPassword
     * The password for login
     *
     * @return
     * The complete login message that can be HTTP Posted
     *
     * @throws UnsupportedEncodingException
     * Any problems during URL encoding
     */
    private String preparePostContent(String loginUserName) throws UnsupportedEncodingException
    {
        // Encode the user name and password to UTF-8 encoding standard
        // throws UnsupportedEncodingException
        String encodedLoginUserName = URLEncoder.encode(loginUserName, "UTF-8");
        //Hidden fields also used for verification
        String viewState = URLEncoder.encode("/wEPDwULLTIxMTU1NzMyNDgPZBYCAgMPZBYCAgkPPCsADQBkGAEFAmd2D2dkNyI00Mr5sftsfyEsC6YBMNkj9mI=", "UTF-8");
        String eventvalidation = URLEncoder.encode("/wEWBAK2l8HDAQKQoJfyDwK6z/31BQLCi9reA4vQxgNI61f/aeA7+HYbm+y+7y3g", "UTF-8");

        String content = "__VIEWSTATE="+viewState+"&amp;__EVENTVALIDATION="+eventvalidation+"&amp;tbxEmail=&amp;tbxRacerName="+encodedLoginUserName+"&amp;btnSubmit=Submit";
        //System.out.println(content);

        return content;

    }

    /**
     * Makes a HTTP POST to the target URL by using an HttpURLConnection.
     *
     * @param targetUrl
     * The URL to which the HTTP POST is made.
     *
     * @param content
     * The contents which will be POSTed to the target URL.
     *
     * @return
     * The open URLConnection which can be used to read any response.
     *
     * @throws IOException
     */
    public HttpURLConnection doHttpPost(String targetUrl, String content) throws IOException
    {
        HttpURLConnection urlConnection = null;
        DataOutputStream dataOutputStream = null;
        try
        {
            // Open a connection to the target URL
            // throws IOException
            urlConnection = (HttpURLConnection)(new URL(targetUrl).openConnection());

            // Specifying that we intend to use this connection for input
            urlConnection.setDoInput(true);

            // Specifying that we intend to use this connection for output
            urlConnection.setDoOutput(true);

            // Specifying the content type of our post
            urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);

            // Specifying the method of HTTP request which is POST
            // throws ProtocolException
            urlConnection.setRequestMethod("POST");

            // Prepare an output stream for writing data to the HTTP connection
            // throws IOException
            dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());

            // throws IOException
            dataOutputStream.writeBytes(content);
            dataOutputStream.flush();
            dataOutputStream.close();

            return urlConnection;
        }
        catch(IOException ioException)
        {
            System.out.println("I/O problems while trying to do a HTTP post.");
            ioException.printStackTrace();

            // Good practice: clean up the connections and streams
            // to free up any resources if possible
            if (dataOutputStream != null)
            {
                try
                {
                    dataOutputStream.close();
                }
                catch(Throwable ignore)
                {
                    // Cannot do anything about problems while
                    // trying to clean up. Just ignore
                }
            }
            if (urlConnection != null)
            {
                urlConnection.disconnect();
            }

            // throw the exception so that the caller is aware that
            // there was some problems
            throw ioException;
        }
    }

    /**
     * Read response from the URL connection
     *
     * @param urlConnection
     * The URLConncetion from which the response will be read
     *
     * @return
     * The response read from the URLConnection
     *
     * @throws IOException
     * When problems encountered during reading the response from the
     * URLConnection.
     */
    private String readResponse(HttpURLConnection urlConnection) throws IOException
    {

        BufferedReader bufferedReader = null;
        try
        {
            // Prepare a reader to read the response from the URLConnection
            // throws IOException
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            urlConnection.setInstanceFollowRedirects(false);
            urlConnection.connect();
            //System.out.println(urlConnection.getHeaderFields());
            String url = urlConnection.getURL().toString();
            //Gets the custId value from the header
            url = url.substring(url.lastIndexOf("CustID=")+7);

            return url;
        }
        catch(IOException ioException)
        {
            System.out.println("Problems while reading the response");
            ioException.printStackTrace();

            // throw the exception so that the caller is aware that
            // there was some problems
            throw ioException;

        }
        finally
        {
            // Good practice: clean up the connections and streams
            // to free up any resources if possible
            if (bufferedReader != null)
            {
                try
                {
                    // throws IOException
                    bufferedReader.close();
                }
                catch(Throwable ignore)
                {
                    // Cannot do much with exceptions doing clean up
                    // Ignoring all exceptions
                }
            }

        }
    }
}
</code></pre>
