Trying to get vars sent to server in url. (Script works find with manual url). The vars are picked up and show in logs but in the portion of the code I show here, the url is not calling the script: String urlString = SERVER + this.getString(R.string.login_details_url); (Btw, the php file name is spelled correctly in the constant which is called on in other working activities).
I think this may be an issue with the way the methods are set up. And there seems to be valid response from server which accounts for the activated success toast at the end. What’s missing here?
thanks
private void acctinfo(String username, String email, String password, String usertype, String over35, String userid) {
Log.d(DEBUG_TAG, "AcctInfo() entered:" + username + email + password + usertype + over35 + userid); /// These vars show up in logs
try
{
sendAcctInfoToServer(this, username, email, password, usertype, over35, userid);
}
catch(Exception e)
{
e.printStackTrace();
Log.d(DEBUG_TAG, "sendAcctInfoToServer error " , e);
Toast.makeText(mContext, "Failed to Subit New Info", Toast.LENGTH_SHORT).show();
}
}
private void sendAcctInfoToServer(Context context, String username, String email, String password, String usertype, String over35, String userid) throws Exception
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
String urlString = SERVER + this.getString(R.string.login_details_url);
urlString = urlString + "?" + USEREMAIL_VAR + "=" + email;
urlString = urlString + "&" + PASSWORD_VAR + "=" + password;
urlString = urlString + "&" + USERNAME_VAR + "=" + username;
urlString = urlString + "&" + USERID_VAR + "=" + userid;
urlString = urlString + "&" + USERTYPE_VAR + "=" + usertype;
urlString = urlString + "&" + OVER35_VAR + "=" + over35;
urlString = urlString+ "&change_acct=1";
HttpPost httppost = new HttpPost(urlString); //////This url does not get sent
HttpResponse response =httpclient.execute(httppost);
// Check if server response is valid
String result = null;
StatusLine status = response.getStatusLine();
Log.d(DEBUG_TAG, " AcctInfo response.getStatusLine() " + status.getStatusCode());
if (status.getStatusCode() != HttpURLConnection.HTTP_OK) {
result = "Invalid response from server, status code=" + status.toString();
// Failed - so throw
throw new Exception("Please try again" + result); //TODO external string, remove details of error
}
else {
//toast
Toast.makeText(mContext, getString(R.string.toast_acct_info_saved), Toast.LENGTH_SHORT).show(); /// this success toast appears but no vars, of course, are passed to script
finish();
}
}
Try using android Uri object to build your URL.
Maybe your problem is due to a bad character encoding.
To log the response you can create a InputStreamReader from response.getEntity().getContent()
I don’t know which min-sdk version uses your application, but there is an AndroidHttpClient which set lot of default values. My request failed with DefaultHttpClient but succeed with AndroidHttpClient.
I don’t use directly AndroidHttpClient (API8), but I get the code (removing parts for curl).
raw AndroidHttpClient sources
Be careful you can’t use AndroidHttpClient in the main thread, you have to execute the query in a new AsyncTask or in a runnable+thread.