0

I've been working on an android app, and recently got all of the login code working using HttpClient. However, this code is now deprecated and no longer works. The language summary suggests replacing all HttpClient code with HttpURLConnection objects. I'm not sure how to do that though. If anyone could help me change my code to HttpURLConnection to submit a login form and retrieve information, it would be greatly appreciated. here is my original use-to-function code:

String u = params[0];
String p = params[1];
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,Boolean.TRUE);
//client.setRedirectStrategy(new LaxRedirectStrategy());
HttpPost post = new HttpPost("https://home-access.cfisd.net/HomeAccess/Account/LogOn");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("LogOnDetails.Username", u));
list.add(new BasicNameValuePair("LogOnDetails.Password",p));
list.add(new BasicNameValuePair("Database","10"));
HttpResponse response = null;
try{
    post.setEntity(new UrlEncodedFormEntity(list));
    response = client.execute(post);
}
catch(Exception ex)
{
    ex.printStackTrace();
}
Mithun
  • 2,075
  • 3
  • 19
  • 26
TheDarBear
  • 199
  • 1
  • 1
  • 13
  • Take a look at the answer to this question http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – Titus May 23 '15 at 04:20
  • You can refer to this post to convert your code using HttpUrlConnection. http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ – TechnoCrat May 23 '15 at 04:22
  • I tried looking into all of these ideas, and they're all just not quite getting me there. they all terminate with a response code 200, but none will ever continue to the next page after logging in. Is there something more I need to do or that I am missing? – TheDarBear May 23 '15 at 05:34
  • http://stackoverflow.com/questions/1739548/use-httpclient-or-httpurlconnection?rq=1 http://stackoverflow.com/questions/8534342/how-to-switch-from-httpurlconnection-to-httpclient?rq=1 http://stackoverflow.com/questions/8534342/how-to-switch-from-httpurlconnection-to-httpclient?rq=1 – hgoebl May 23 '15 at 08:32

2 Answers2

1

Hey bro get a library like

androids defaults are old and clunky

loopjs asynchttpclient or okhttp or Koushs ion

You do a whole lot of work with 2-3 lines of code

loopjs async http example

RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, responseHandler);

//do something with response
@Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

        }
    });
0

You can get output stream for the connection and write the parameter query string to it.

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setReadTimeout(10000);

conn.setConnectTimeout(15000);

conn.setRequestMethod("POST");

conn.setDoInput(true);

conn.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("firstParam", paramValue1));

params.add(new BasicNameValuePair("secondParam", paramValue2));

params.add(new BasicNameValuePair("thirdParam", paramValue3));

OutputStream os = conn.getOutputStream();

BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));

writer.write(getQuery(params));

writer.flush();

writer.close();

os.close();

conn.connect();

....

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{

    StringBuilder result = new StringBuilder();
boolean first = true;

for (NameValuePair pair : params)
{
    if (first)
        first = false;
    else
        result.append("&");

    result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
    result.append("=");
    result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}

return result.toString();
}
Rajan Bhavsar
  • 1,977
  • 11
  • 25