gpt4 book ai didi

java - 通过php在java(android studio)中获取数据

转载 作者:行者123 更新时间:2023-12-02 04:12:45 26 4
gpt4 key购买 nike

如标题所示,只想从我的 php 文件中获取数据并检查输入的电子邮件、密码是否是注册用户的。如果是,则重定向到成功页面(TimelineActivity)

因此查看了这个:- How to get values from mysql database using php script in android

这是我的LoginBasicActivity.java

public class LoginBasicActivity extends AppCompatActivity {
public AutoCompleteTextView uemail;
public EditText upassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_basic);
uemail = (AutoCompleteTextView) findViewById(R.id.emailBasic);
upassword = (EditText) findViewById(R.id.passwordBasic) ;
Button buttonLog = (Button) findViewById(R.id.buttonLog);
buttonLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String tem;
String email = uemail.getText().toString();
String result = null;
String password = upassword.getText().toString() ;
// HttpClientBuilder.create();

// As HttpClient client = new DefaultHttpClient(); is not used anymore
HttpClient client = HttpClients.createDefault();

tem = "http://www.example_domain.com/app_folder/verify-user.php?username="+email+"&password="+password;
HttpGet request = new HttpGet(tem);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
result = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
}

if (result.equals("0") == true) {
Toast.makeText(getApplicationContext(), "Sorry try again", Toast.LENGTH_LONG).show();

} else {
Toast.makeText(getApplicationContext(), "Welcome Client", Toast.LENGTH_LONG).show();
Intent myIntt = new Intent(view.getContext(), TimelineActivity.class);
startActivityForResult(myIntt, 0);

}
}

});
}
}

这是verify-user.php

 <?php

mysql_connect("localhost","user_name","user_password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
$SelectQuery="select * from table_name where C_Username='$username' and C_Password='$password'";
$result=mysql_query($SelectQuery) or die(mysql_error());
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if($count==0)
echo "0";
else
echo $row['c_id'];
?>

在此之后,我在 build.gradle 中遇到了一些文件重复问题,因此在 build.gradle 中添加了此问题

 android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
}
}`

现在,当按下登录按钮时,对话框显示“不幸的是 app_name 已停止”

Logcat :- https://www.dropbox.com/s/63cv806z4y8h9my/2015-11-10-05-01-44%5B1%5D.txt?dl=0

我对 Android-Java 有点陌生。

如何解决这个问题?有人可以解释一下正确的语法,哪里出了问题吗?

最佳答案

您正在通过主线程发出网络(api)请求,这会导致问题。从线程进行 api 命中或使用 AsyncTask 或 Loader。另请检查您是否已向 list 文件添加互联网权限。如果没有,请在 list 文件中添加以下行。

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

编辑:您可以在 Activity 中创建一个如下所示的内部类,并使用 new LoginAsyncTask().execute(); 在按钮单击时调用它;

public class LoginAsyncTask extends AsyncTask<Void, Void, String> { protected String doInBackground(Void... params) {    //Run in background thread(can not access UI and not able to perform UI related operations here).        //make your network hit here and return the result which is string in your case.        //The onPostExecute Method is get called automatically after this method returns.         ...     }      protected void onPreExecute() {    //run on main thread(can access UI)        //do some initialization here,if needed, before network hit.      ...     }     protected void onPostExecute(String result) {    //Method run on main thread,can access UI.result is the value returned by doInBackground method.        //Put a check over result and perform the further operation accordingly.        .....     }}

关于java - 通过php在java(android studio)中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625944/

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