2015-09-04 92 views
-1

如何在PHP和MySQL中进行登录和注册?
我需要的代码示例,希望有人能帮助我如何使用php和mysql进行登录和注册?

这里是我的loginfragment代码:

Package com.example.administrator.mosbeau; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by Administrator on 9/1/2015. 
*/ 
public class LoginFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.loginlayout, container, false); 
    } 
} 

,这里是我的loginlayout

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:fillViewport="false"> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView4" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/logo" 
     android:maxHeight="54dp" 
     android:maxWidth="322dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="100dp" 
     android:layout_marginRight="100dp" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView5" 
     android:layout_below="@+id/imageView4" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/lock" 
     android:layout_marginLeft="150dp" 
     android:layout_marginRight="150dp" 
     android:layout_marginTop="-60dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtEmail" 
     android:layout_below="@+id/imageView5" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="30dp" 
     android:hint="Email" 
     android:background="#ffeeeeee" 
     android:padding="10dp" 
     android:layout_marginTop="-30dp" 
     android:textColor="#ff6d6e71" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/txtPassword" 
     android:layout_below="@+id/txtEmail" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="30dp" 
     android:textColor="#ff6d6e71" 
     android:background="#ffeeeeee" 
     android:padding="10dp" 
     android:hint="Password" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign in" 
     android:id="@+id/btnSignin" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="30dp" 
     android:textColor="#ff6d6e71" 
     android:layout_marginTop="10dp" 
     android:layout_below="@+id/txtPassword" 
     android:longClickable="false" 
     android:clickable="true" 
     android:background="#fffed4d4" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Forgot your Password ?" 
     android:id="@+id/textForgot" 
     android:layout_below="@+id/btnSignin" 
     android:layout_alignLeft="@+id/btnSignin" 
     android:layout_marginTop="10dp" 
     android:textColor="#ff607d8b" 
     android:textSize="18dp" 
     android:clickable="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sign Up" 
     android:id="@+id/textSignup" 
     android:layout_alignBottom="@+id/textForgot" 
     android:layout_alignRight="@+id/btnSignin" 
     android:textColor="#ff607d8b" 
     android:textSize="18dp" 
     android:layout_marginTop="10dp" 
     android:clickable="true" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Login with facebook" 
     android:id="@+id/btnLoginfb" 
     android:layout_below="@+id/textForgot" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="30dp" 
     android:background="#ff2385ff" 
     android:textColor="#ffffffff" 
     android:clickable="true" 
     android:focusableInTouchMode="false" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Help" 
     android:id="@+id/btnLoginhelp" 
     android:layout_below="@+id/btnLoginfb" 
     android:layout_alignLeft="@+id/btnLoginfb" 
     android:layout_marginTop="10dp" 
     android:background="#fffed4d4" 
     android:textColor="#ff6d6e71" 
     android:clickable="true" 
     android:layout_marginBottom="20dp" 
     android:focusableInTouchMode="false" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Skip" 
     android:id="@+id/btnLoginskip" 
     android:background="#fffed4d4" 
     android:textColor="#ff6d6e71" 
     android:layout_alignTop="@+id/btnLoginhelp" 
     android:layout_alignRight="@+id/btnLoginfb" 
     android:clickable="true" 
     android:layout_marginBottom="20dp" 
     android:focusableInTouchMode="false" /> 

</RelativeLayout> 
</ScrollView> 

我是新到android,所以我真的不知道该怎么做。
我已经开发了登录并通过以下教程注册PHP和MySQL,但它使用活动。

回答

0

你问样一个复杂的问题,特别是没有能够看到你的服务器端代码。

既然你提到需要的源代码,这里是官方Android开发人员训练与源代码示例,http://developer.android.com/training/basics/network-ops/connecting.html

我会复制在这里的相关代码,

public void myClickHandler(View view) { 
    ... 
    ConnectivityManager connMgr = (ConnectivityManager) 
     getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    if (networkInfo != null && networkInfo.isConnected()) { 
     // fetch data 
    } else { 
     // display error 
    } 
    ... 
} 
public class HttpExampleActivity extends Activity { 
    private static final String DEBUG_TAG = "HttpExample"; 
    private EditText urlText; 
    private TextView textView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     urlText = (EditText) findViewById(R.id.myUrl); 
     textView = (TextView) findViewById(R.id.myText); 
    } 

    // When user clicks button, calls AsyncTask. 
    // Before attempting to fetch the URL, makes sure that there is a network connection. 
    public void myClickHandler(View view) { 
     // Gets the URL from the UI's text field. 
     String stringUrl = urlText.getText().toString(); 
     ConnectivityManager connMgr = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) { 
      new DownloadWebpageTask().execute(stringUrl); 
     } else { 
      textView.setText("No network connection available."); 
     } 
    } 

    // Uses AsyncTask to create a task away from the main UI thread. This task takes a 
    // URL string and uses it to create an HttpUrlConnection. Once the connection 
    // has been established, the AsyncTask downloads the contents of the webpage as 
    // an InputStream. Finally, the InputStream is converted into a string, which is 
    // displayed in the UI by the AsyncTask's onPostExecute method. 
    private class DownloadWebpageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 

      // params comes from the execute() call: params[0] is the url. 
      try { 
       return downloadUrl(urls[0]); 
      } catch (IOException e) { 
       return "Unable to retrieve web page. URL may be invalid."; 
      } 
     } 
     // onPostExecute displays the results of the AsyncTask. 
     @Override 
     protected void onPostExecute(String result) { 
      textView.setText(result); 
     } 
    } 
    ... 
} 
// Given a URL, establishes an HttpUrlConnection and retrieves 
// the web page content as a InputStream, which it returns as 
// a string. 
private String downloadUrl(String myurl) throws IOException { 
    InputStream is = null; 
    // Only display the first 500 characters of the retrieved 
    // web page content. 
    int len = 500; 

    try { 
     URL url = new URL(myurl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(10000 /* milliseconds */); 
     conn.setConnectTimeout(15000 /* milliseconds */); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     // Starts the query 
     conn.connect(); 
     int response = conn.getResponseCode(); 
     Log.d(DEBUG_TAG, "The response is: " + response); 
     is = conn.getInputStream(); 

     // Convert the InputStream into a string 
     String contentAsString = readIt(is, len); 
     return contentAsString; 

    // Makes sure that the InputStream is closed after the app is 
    // finished using it. 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 
} 
... 
    InputStream is = null; 
    ... 
    Bitmap bitmap = BitmapFactory.decodeStream(is); 
    ImageView imageView = (ImageView) findViewById(R.id.image_view); 
    imageView.setImageBitmap(bitmap); 
... 
// Reads an InputStream and converts it to a String. 
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException { 
    Reader reader = null; 
    reader = new InputStreamReader(stream, "UTF-8");   
    char[] buffer = new char[len]; 
    reader.read(buffer); 
    return new String(buffer); 
} 

做完这几虽然我想指出一些可能会咬你的东西,当你试图实现这个

  • Android需要你在后台线程中执行任何调用Web服务器的操作。 http://developer.android.com/training/multiple-threads/index.html

  • 您可能需要某种身份验证,特别是如果您使用的是PHP端, Drupal。我会发现你的PHP框架是否需要使用CRLF令牌或OAuth或类似方案的会话身份验证。验证将包括三个步骤:

**登陆

**检索一个cookie或令牌

**发送令牌连同您的内容在你的头