私はログインアクティビティを作成しました。ログイン成功後、正常に動作しています。 "profile"アクティビティが開かれていますが、mysqlデータベースに格納されているログインしたユーザの詳細をロードしたいと思っています。誰も私にそれをやる方法を助けることができますか?ログインが成功した後、別のアクティビティ(プロファイルアクティビティ)でユーザのデータベースを読み込む方法は?私はプロフィールページを作成しようとしています
マイBackgroundworker.javaファイル
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.43.59/login.php";
String register_url = "http://192.168.43.59/register.php";
if (type.equals("login")) {
try {
String username = params[1];
String password = params[2];
URL url = new URL (login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("pass_word","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (type.equals("register")){
try {
String name = params[1];
String surname = params[2];
String username = params[3];
String password = params[4];
String address = params[5];
String pinArea_code = params[6];
String email = params[7];
String phone = params[8];
URL url = new URL (register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("reg_name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("reg_surname","UTF-8")+"="+URLEncoder.encode(surname,"UTF-8")+"&"
+URLEncoder.encode("reg_user","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("reg_pass","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
+URLEncoder.encode("reg_add","UTF-8")+"="+URLEncoder.encode(address,"UTF-8")+"&"
+URLEncoder.encode("reg_pin","UTF-8")+"="+URLEncoder.encode(pinArea_code,"UTF-8")+"&"
+URLEncoder.encode("reg_mail","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
+URLEncoder.encode("reg_ph","UTF-8")+"="+URLEncoder.encode(phone,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
if(result.contains("login success")){
Intent ilogin = new Intent(context, Profile.class);
context.startActivity(ilogin);
alertDialog.dismiss();
} else if (result.contains("Account Created")){
Intent iregister = new Intent(context, about_us.class);
context.startActivity(iregister);
alertDialog.dismiss();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
マイMainActivity.Javaファイル
package com.quickhomeservice.qhs.astudio;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText UsernameET, PasswordET;
//String username, password;
private TextView button_reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsernameET = (EditText)findViewById(R.id.editText_user);
PasswordET = (EditText)findViewById(R.id.editText_pass);
ActionBar ab = getSupportActionBar();
//ab.setLogo(R.mipmap.logo_mini86);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowHomeEnabled(true);
OnClickButtonListner();
}
public void OnLogin (View view){
String username = UsernameET.getText().toString();
String password = PasswordET.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}
public void OnClickButtonListner(){
button_reg=(TextView) findViewById(R.id.register_link);
button_reg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.quickhomeservice.qhs.astudio.registraion_form");
startActivity(intent);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_activity, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.Contact_id:
Intent intent1 = new Intent(this, contact_us.class);
startActivity(intent1);
return true;
case R.id.About_id:
Intent intent2 = new Intent(this, about_us.class);
startActivity(intent2);
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?php
require "conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["pass_word"];
$mysql_qry = "select * from user_data where username like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn ,$mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "login success";
}
else{
echo "login failed";
}
?>
私はスクリプトの仲間に新しいです、私はそれを正しく取得していません、可能であればサンプルファイルを作成できますか? –