简体   繁体   English

将注册数据从用户发送到服务器

[英]Sending sign up data from user to server

EDIT: I finally figured out where the problem is. 编辑:我终于弄清楚问题出在哪里。 I keep getting an IOException on this line response = httpClient.execute(httpPost). 我不断在此行响应上获得IOException = httpClient.execute(httpPost)。

However, the normal problems that will cause this error (the ones that I searched online, like the url being wrong and such) are not the problem here. 但是,将导致此错误的正常问题(我在网上搜索的问题,例如url错误等等)不是这里的问题。 The url is correct. 网址正确。 Can anyone help or list all of the possible reasons I would get this error. 任何人都可以帮忙或列出所有可能导致此错误的原因。 Thank you. 谢谢。


I know this question has been asked several times, and I actually looked at the answers given for those questions in order to originally figure out how to do this but for some reason it's not working and I don't know why. 我知道这个问题已经问过几次了,实际上我查看了针对这些问题的答案,以便最初弄清楚该怎么做,但是由于某种原因,它不起作用,我也不知道为什么。

I have an app that requires a user to sign up. 我有一个需要用户注册的应用程序。 After they sign up I send the information they inputted to the server. 他们注册后,我会将他们输入的信息发送到服务器。

Android code: Android代码:

//This is the method called when user presses submit button. The variables not declared
//are global
public void registerUser(View v){
    context = getApplicationContext();

    username = ((EditText) this.findViewById(R.id.username)).getText().toString();
    email = ((EditText) this.findViewById(R.id.email)).getText().toString();
    password=((EditText)this.findViewById(R.id.password)).getText().toString();

    String[] params = {username,email,password};
    (new SendRegisterInfo(this.getApplicationContext())).execute(params);

    Toast t = Toast.makeText(context, "Thank you for Signing up "+username, Toast.LENGTH_SHORT);
    t.show();
    //Start Main Page Activity. Page they'll see everytime they login
    Intent i = new Intent(this,HomeActivity.class);
    startActivity(i);

}


public class SendRegisterInfo extends AsyncTask<String,Void,Void>{

private String tag = "SendRegisterInfo";
private InputStream is;
private Context c;

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
}

public SendRegisterInfo(Context c){
    this.c = c;
}

@Override
protected Void doInBackground(String... params) {
    String URI="http://www.mysite.com/registerUser.php";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URI);
    HttpResponse response;

    String username,email,password;
    username = params[0];
    email = params[1];
    password = params[2];


    try {

        ArrayList<NameValuePair> submit = new ArrayList<NameValuePair>(2);
        submit.add(new BasicNameValuePair("username",username));
        submit.add(new BasicNameValuePair("email",email));
        submit.add(new BasicNameValuePair("password",password));
        httpPost.setEntity(new UrlEncodedFormEntity(submit));

        response = httpClient.execute(httpPost);
        Log.i(tag,response.getStatusLine().toString());

    } catch (ClientProtocolException e) {
        Log.e(tag, "Error in http connection "+e.toString());

        e.printStackTrace();
    } catch (IOException e) {
        Log.e(tag, "Error in http connection "+e.toString());

        e.printStackTrace();
    }catch (Exception e){
        Log.e(tag, "Error in http connection "+e.toString());

        e.printStackTrace();
    }

    return null;
}


}

Manifest File: 清单文件:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

PHP Code: PHP代码:

<?php
$username =  $_POST["username"];
$email =  $_POST["email"];
$pswrd = $_POST["password"];
$score = 0;

$user = "root";
$password="pword";
$database = "databasename";

mysql_connect(localhost,$user,$password);

mysql_select_db($database) or die("Unable to Select Database");

$query="INSERT INTO Players VALUES ('','$username','$email','$pswrd','','$score')";

mysql_query($query);

mysql_close();

?>

You have an error reading the (String... params) argument of the doInBackground() method. 读取doInBackground()方法的(String ... params)参数时出错。

When you start the AsyncTask: 当您启动AsyncTask时:

 String[] params = {username,email,password};

When you read the arguments 当您阅读参数时

String username,email,weight,password;
username = params[0];
email = params[1];
password = params[3];  // should be: password = params[2];

If this isn't works, adds a mySql query asking for the last insert id on the php program and print the result to get the response in the client: 如果这不起作用,则添加一个mySql查询,以询问php程序上的最后一个插入ID,并打印结果以在客户端中获得响应:

response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();    
Log.i(tag,EntityUtils.toString(entity).toString());

So you can see if you are inserting or not on the db. 因此,您可以查看是否要在数据库上进行插入。

This is your issue: 这是您的问题:

RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

You're attempting to show a Toast from a class that isn't designed to show a Toast. 您正在尝试从未设计为展示Toast的类中展示Toast。 Or, perhaps, isn't prepared to show a tost. 或者,也许不准备展示托斯。 What is the class containing the method registerUser 包含方法registerUser的类是什么

Please check the code below. 请检查下面的代码。

  • Add function name and key to the request along with your data. 将函数名称和键以及您的数据添加到请求中。

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); nameValuePairs.add((NameValuePair) new BasicNameValuePair("f", "yourFunctionName."+methodName)); nameValuePairs.add((NameValuePair) new BasicNameValuePair("u", "a0ff8a6a0a831ec25cf4de6c730be54c")); //u is the key used by server to identify valid request. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM