2011-03-14 15 views
0

试图让vars发送到服务器的url。 (脚本可以通过手动链接找到)。变量被拾取并显示在日志中,但在我显示的代码部分中,url不调用脚本:String urlString = SERVER + this.getString(R.string.login_details_url); (顺便说一句,php文件名拼写正确的是在其他工作活动中调用的常量)。Android代码中的扭曲Java方法需要良好的眼球

我认为这可能是一个方法设置的问题。并且似乎有来自服务器的有效响应,其在最后占据了激活的成功吐司。这里缺少什么?

感谢

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

回答

0

尝试使用Android的Uri对象建立自己的网址。

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

也许你的问题是由于错误的字符编码。

要登录您可以创建response.getEntity()一个InputStreamReader的响应。的getContent()
我不知道是哪个分钟-SDK版本使用你的应用程序,但有一个AndroidHttpClient里面设置很多默认值。我的请求失败与DefaultHttpClient,但成功与AndroidHttpClient。
我不直接使用AndroidHttpClient(API8),但我得到的代码(删除卷曲部分)。 raw AndroidHttpClient sources
小心你不能在主线程中使用AndroidHttpClient,你必须在新的AsyncTask或可运行+线程中执行查询。

+0

谢谢你的回复,吕克。有趣的是,我发布的这部分代码在Login.java中是“镜像的”,并且没有任何问题。为什么它在这个文件AcctInfo.java中不起作用,这很奇怪。思考? – artworthy 2011-03-14 18:44:05

1

肯定采取LUC的建议,为建设开放的,因为它会导致对,将有你的硬物敲打你的头拼写或编码错误的机会较低。我发现某个问题可能比一个明确的问题更为严重,并且可能是问题的根源,具体取决于远程服务器的设置方式。

您正在创建一个HttpPost对象,该对象使用设置为POST的HTTP动词发出请求。在这些情况下,您传递的参数通常会被编码到POST正文中,而不是在URL本身中。当您在URL中传递参数时,请求通常是GET请求(使用Android中的HttpGet构建)。还有就是,如果你的服务器需要一个POST,它不是在URL中为您寻找的参数的机会,而如果看在URL,它的预期,而不是一个GET。

要编码您的参数在后的身体(如果你需要它),这样做:

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

希望帮助!

+0

它做到了。谢谢。使用GETS管理获取发送到服务器的URL,顺便说一句,但会重新编码为对值POSTS。 – artworthy 2011-03-15 14:15:53