开发者

Twisted Java methods in Android code needs good eyeballs

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();
    }
}


Definitely take luc's suggestion for building the Uri as it will result in a lower chance for spelling or encoding errors that will have you banging your head on hard objects. I see something that is more of a probable issue than a definite bug, and might be the source of your problem depending on how the remote server is set up.

You are creating an HttpPost object, which makes a request with the HTTP verb set to POST. In these cases, it is typical for the parameters you pass to be encoded into the POST body, not in the URL itself. When you pass parameters in the URL, the request is typically a GET request (built using HttpGet in Android). There is a chance that, if your server expects a POST, it's not looking in the URL for your parameters, and if it is looking in the URL, it's expecting a GET instead.

To encode your parameters in the body of a POST (if you need it), do something like this:

HttpClient client = new DefaultHttpClient();
String urlString = SERVER + this.getString(R.string.login_details_url);
HttpPost request = new HttpPost(urlString);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair(USEREMAIL_VAR,email));
parameters.add(new BasicNameValuePair(PASSWORD_VAR,password));
//...etc...
request.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = client.execute(request);

Hope that Helps!


Try using android Uri object to build your URL.

String baseUrl = getResources().getString(R.string.login_details_url);
Uri.Builder builder = Uri.parse(baseUrl).buildUpon()
    .appendQueryParameter(USEREMAIL_VAR, email)
    .append...;
HttpPost httppost = new HttpPost(builder.build());
HttpResponse response = httpclient.execute(httppost);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜