Android 代码中扭曲的 Java 方法需要足够的关注
尝试将变量发送到 url 中的服务器。 (脚本可以使用手动 url 进行查找)。变量被拾取并显示在日志中,但在我在这里显示的代码部分中,url 没有调用脚本: String urlString = SERVER + this.getString(R.string.login_details_url); (顺便说一句,php 文件名在其他工作活动中调用的常量中拼写正确)。
我认为这可能是方法设置方式的问题。服务器似乎有有效的响应,这说明了最后激活的成功 toast。这里缺少什么?
谢谢
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一定要采纳 luc 关于构建 Uri 的建议,因为它会降低出现拼写或编码错误的可能性,而这些错误会让你用头撞到坚硬的物体上。我看到的问题更像是一个可能的问题,而不是一个明确的错误,并且可能是问题的根源,具体取决于远程服务器的设置方式。
您正在创建一个
HttpPost
对象,该对象使用设置为 POST 的 HTTP 谓词发出请求。在这些情况下,您传递的参数通常会编码到 POST 正文中,而不是 URL 本身中。当您在 URL 中传递参数时,请求通常是 GET 请求(在 Android 中使用HttpGet
构建)。有可能,如果您的服务器需要 POST,它不会在 URL 中查找您的参数,而如果它在 URL 中查找,则它会期待一个 GET。要将参数编码到 POST 正文中(如果需要),请执行以下操作:
希望有帮助!
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 usingHttpGet
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:
Hope that Helps!
尝试使用 android Uri 对象来构建您的 URL。
也许您的问题是由于字符编码错误造成的。
要记录响应,您可以从response.getEntity().getContent()创建一个InputStreamReader
我不知道哪个 min-sdk 版本使用您的应用程序,但有一个 AndroidHttpClient 设置了很多默认值。我的请求使用 DefaultHttpClient 失败,但使用 AndroidHttpClient 成功。
我不直接使用AndroidHttpClient(API8),但我得到了代码(删除了curl的部分)。
原始 AndroidHttpClient 源
请注意,您不能在主线程中使用 AndroidHttpClient,您必须在新的 AsyncTask 或可运行+线程中执行查询。
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.