Android : Android httpclient login to Rails server

on Wednesday, September 10, 2014


I started to learn about how to make an android application. I tried to connect my app to rails server by using httpclient, however I cannot catch a point on how to connect between app and the remote server. Here is part of my code, and I matched id form inside "BasicNameValuePair" with html id values. This is my urgent thing, so if possible, please suggest some ways to solve this problem. Plus, let me know how to check whether login is successful or not. Thank you.


class SendPost extends AsyncTask {



protected String doInBackground(Void... unused) {
String content = executeClient();
return content;
}

protected void onPostExecute(String result) {

}


public String executeClient() {
ArrayList<NameValuePair> post = new ArrayList<NameValuePair>();
post.add(new BasicNameValuePair("user_name", "SallyCook"));
post.add(new BasicNameValuePair("user_email", "domain@ppls.kr"));
post.add(new BasicNameValuePair("user_password", "add123456"));
post.add(new BasicNameValuePair("user_password_confirmation", "add123456"));
post.add(new BasicNameValuePair("user_phone", "01013089579"));


HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
System.out.println(params);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);

HttpPost httpPost = new HttpPost("http://www.ppls.kr/users/sign_up");

try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(post, "UTF-8");
httpPost.setEntity(entity);
HttpResponse responsePost = client.execute(httpPost);
System.out.println(responsePost.getStatusLine());
HttpEntity resEntity=responsePost.getEntity();


if (resEntity != null) {
Log.w("RESPONSE", EntityUtils.toString(resEntity));
}
return EntityUtils.getContentCharSet(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


}


0 comments:

Post a Comment