gpt4 book ai didi

java - 安卓PHP用户注册

转载 作者:行者123 更新时间:2023-11-29 09:01:57 26 4
gpt4 key购买 nike

我是 android 的新手,我如何将值从 android 插入到 php mysql 我尝试了以下代码它没有显示任何错误但不工作。谁能帮我。谁能纠正我的错误。

public class MainActivity extends Activity{

EditText username, email, password;
Button btn;
HttpResponse response;

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

username = (EditText) findViewById(R.id.usr_name);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);

btn = (Button) findViewById(R.id.regbtn);
btn.setOnClickListener(new View.OnClickListener(){


public void onClick(View v){

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest.php");

try{

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));
nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);

}catch(Exception e){
e.printStackTrace();
}
}
});
}

}

最佳答案

我在我的本地 VM 上测试了您的示例,对我来说有 "06-01 00:22:17.671: W/System.err(2024): android.os.NetworkOnMainThreadException" 因为我之前评论过。

不要在主线程上做网络。

关于如何使用异步任务的(其他)示例是 postet here .

异步任务与主线程并行执行,因此它们不会阻塞主线程。阻塞主线程是不好的,因为它会阻止用户界面重新绘制/界面无法处理输入。
因此 android 会抛出 NetworkOnMainThreadException 以防止您在主线程上进行联网(一项耗时的任务)。

可以找到示例实现 here .

关于java - 安卓PHP用户注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com