2016-05-03 12 views
-1

私は理由
THESコードを修正するすべてのヘルプが何であるかを知らない私はこのコードを持っていますが、それは
仕事doesntのアンドロイド
にPHPでログインしたいです?どのようにアンドロイドでphp sqlでログインできますか?

package com.ust.autism; 
 

 

 
import android.app.Activity; 
 
import android.app.ProgressDialog; 
 
import android.content.Intent; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.EditText; 
 
import android.widget.TextView; 
 
import android.widget.Toast; 
 

 
import org.apache.http.HttpResponse; 
 
import org.apache.http.NameValuePair; 
 
import org.apache.http.client.HttpClient; 
 
import org.apache.http.client.ResponseHandler; 
 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
 
import org.apache.http.client.methods.HttpPost; 
 
import org.apache.http.impl.client.BasicResponseHandler; 
 
import org.apache.http.impl.client.DefaultHttpClient; 
 
import org.apache.http.message.BasicNameValuePair; 
 

 
import java.util.ArrayList; 
 
import java.util.List; 
 

 

 
public class OfMainActivity extends Activity { 
 
    EditText et, pass; 
 
    TextView text1; 
 
    String send_num; 
 
    HttpPost httppost; 
 
    HttpResponse response; 
 
    HttpClient httpclient; 
 
    List<NameValuePair> nameValuePairs; 
 
    ProgressDialog dialog = null; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     et = (EditText) findViewById(R.id.user); 
 
     pass = (EditText) findViewById(R.id.pass); 
 
     text1 = (TextView) findViewById(R.id.text1); 
 
    } 
 

 
    public void onClick(View v) { 
 
     dialog = ProgressDialog.show(OfMainActivity.this, "", 
 
       "التحقق من اسم المستخدم ...", true); 
 
     new Thread(new Runnable() { 
 
      public void run() { 
 
       login(); // نفذ الدالة داخل الثريد اي في خلفية التطبيق .. 
 
      } 
 
     }).start(); 
 
    } 
 

 
    void login() { 
 
     try { 
 

 
      httpclient = new DefaultHttpClient(); 
 
      httppost = new HttpPost("http://localhost/sync.php"); // رابط مسار ملف الربط سواء كان على السيرفر او استضافة إلخ 
 
      //اضافة البيانات 
 
      nameValuePairs = new ArrayList(2); 
 
      // المتغيرات التي كتبها المستخدم 
 
      nameValuePairs.add(new BasicNameValuePair("username", et.getText().toString().trim())); 
 
      nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString().trim())); 
 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
 
      //تنفيذ HTTP 
 
      response = httpclient.execute(httppost); 
 
      // جلب الرسالة التي يظهرها ملف الربط مع القاعدة 
 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
 
      final String response = httpclient.execute(httppost, responseHandler); 
 
      runOnUiThread(new Runnable() { 
 
       public void run() { 
 
        text1.setText(response); // ضع الرسالة التي تأتي من ملف الربط مكان النص 
 
        dialog.dismiss(); 
 
       } 
 
      }); 
 

 
      if (response.equalsIgnoreCase("User Found")) { // لاحظ الرسالة هنا هي نفسها التي موجودة في ملف الربط مع القاعدة 
 
       runOnUiThread(new Runnable() { 
 
        public void run() { 
 
         Toast.makeText(OfMainActivity.this, "تم تسجيل الدخول بنجاح", Toast.LENGTH_SHORT).show(); 
 
        } 
 
       }); 
 
// اذا تم تسجيل الدخول بنجاح أذهب إلى الصفحة الرئيسية وخذ معك اسم المستخدم 
 
       Intent i = new Intent(OfMainActivity.this, Elevel.class); 
 
       send_num = et.getText().toString(); 
 
       i.putExtra("text", send_num); 
 
       startActivity(i); 
 
      } else { 
 
      } 
 

 
     } catch (Exception e) { 
 
     } 
 
    } 
 
}

+2

はlogcatの詳細を投稿 –

+0

私たちがすることはできませんあなたの問題がどこにあるのかわからない場合は、何をすべきかを指摘してください。とにかくこれを行う方法については、アンドロイドハイブの良いチュートリアルがあります:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ – Ricardo

答えて

0

あなたのポストサービスでのログインのためにこのコードを試してみてください...

private class LoginAysnc extends AsyncTask<Void,Void,Void> 
    { 
     private ProgressDialog regDialog=null; 
     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
      regDialog=new ProgressDialog(this); 
      regDialog.setTitle(getResources().getString(R.string.app_name)); 
      regDialog.setMessage(getResources().getString(R.string.app_pleasewait)); 
      regDialog.setIndeterminate(true); 
      regDialog.setCancelable(true); 
      regDialog.show(); 
     }  
     @Override 
     protected Void doInBackground(Void... params) { 
      try 
      { 
       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
         postParameters.add(new BasicNameValuePair("username", 
           et.getText().toString().trim())); 
         postParameters.add(new BasicNameValuePair("password", 
           pass.getText().toString())); 

         String response = null; 
         try { 
          response = SimpleHttpClient 
            .executeHttpPost("http://localhost/sync.php", 
              postParameters); 
          res = response.toString(); 

          return res; 

         } catch (Exception e) { 
          e.printStackTrace(); 
          errorMsg = e.getMessage(); 
         } 
        } 
       }).start(); 
       try { 
        Thread.sleep(3000); 


       // error.setText(resp); 
        if (null != errorMsg && !errorMsg.isEmpty()) { 

        } 
       } catch (Exception e) { 
       } 

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

     @Override 
     protected void onPostExecute(Void result) 
     { 
      super.onPostExecute(result); 
      if(regDialog!=null) 
      { 

       regDialog.dismiss(); 

       System.out.println("PostId:-"+str_postid); 

       } 

    // do what u do 
    } 

SimpleHttpClient.javaが

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.conn.params.ConnManagerParams; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 

public class SimpleHttpClient { 
/** The time it takes for our client to timeout */ 
    public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds 

    /** Single instance of our HttpClient */ 
    private static HttpClient mHttpClient; 

    /** 
    * Get our single instance of our HttpClient object. 
    * 
    * @return an HttpClient object with connection parameters set 
    */ 
    private static HttpClient getHttpClient() { 
    if (mHttpClient == null) { 
     mHttpClient = new DefaultHttpClient(); 
     final HttpParams params = mHttpClient.getParams(); 
     HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT); 
     HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT); 
     ConnManagerParams.setTimeout(params, HTTP_TIMEOUT); 
    } 
    return mHttpClient; 
    } 

    public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpPost request = new HttpPost(url); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(formEntity); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
     sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return result; 
    } 
    finally { 
     if (in != null) { 
     try { 
      in.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
    } 


    public static String executeHttpatch(String url, ArrayList<NameValuePair> postParameters) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpPost request = new HttpPost(url); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(formEntity); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
     sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return result; 
    } 
    finally { 
     if (in != null) { 
     try { 
      in.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
    } 

    /** 
    * Performs an HTTP GET request to the specified url. 
    * 
    * @param url The web address to post the request to 
    * @return The result of the request 
    * @throws Exception 
    */ 
    public static String executeHttpGet(String url) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI(url)); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
     sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return result; 
    } 
    finally { 
     if (in != null) { 
     try { 
      in.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
    } 
}