2015-09-05 46 views
-1

我是新手在這裏。我想將我的android應用程序連接到wampserver數據庫。我在這個link上找到了一個教程,但是我應該在哪裏找到我的serverUrl在wampserver?它應該看起來像這樣:private final String serverUrl = "your path here"那麼我在哪裏可以找到"your path here"Android to Wamp使用Android Studio和PHP連接的服務器

這裏是我的代碼:

MainActivity.java

package inducesmile.com.androidloginandregistration; 

import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 


public class MainActivity extends ActionBarActivity { 

    protected EditText username; 
    private EditText password; 
    protected String enteredUsername; 
    private final String serverUrl = "your path here"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     username = (EditText)findViewById(R.id.username_field); 
     password = (EditText)findViewById(R.id.password_field); 
     Button loginButton = (Button)findViewById(R.id.login); 
     Button registerButton = (Button)findViewById(R.id.register_button); 

     loginButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       enteredUsername = username.getText().toString(); 
       String enteredPassword = password.getText().toString(); 

       if(enteredUsername.equals("") || enteredPassword.equals("")){ 
        Toast.makeText(MainActivity.this, "Username or password must be filled", Toast.LENGTH_LONG).show(); 
        return; 
       } 
       if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){ 
        Toast.makeText(MainActivity.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show(); 
        return; 

       // request authentication with remote server4 
       AsyncDataClass asyncRequestObject = new AsyncDataClass(); 
       asyncRequestObject.execute(serverUrl, enteredUsername,  enteredPassword); 

      } 
     }); 

     registerButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this,   RegisterActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
     return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    private class AsyncDataClass extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      HttpParams httpParameters = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); 
      HttpConnectionParams.setSoTimeout(httpParameters, 5000); 

      HttpClient httpClient = new DefaultHttpClient(httpParameters); 
      HttpPost httpPost = new HttpPost(params[0]); 

      String jsonResult = ""; 
      try { 
       List<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("username", params[1])); 
       nameValuePairs.add(new BasicNameValuePair("password", params[2])); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpClient.execute(httpPost); 
       jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return jsonResult; 
     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 
     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      System.out.println("Resulted Value: " + result); 
     if(result.equals("") || result == null){ 
      Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     int jsonResult = returnParsedJsonObject(result); 
     if(jsonResult == 0){ 
      Toast.makeText(MainActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     if(jsonResult == 1){ 
      Intent intent = new Intent(MainActivity.this, LoginActivity.class); 
      intent.putExtra("USERNAME", enteredUsername); 
      intent.putExtra("MESSAGE", "You have been successfully login"); 
      startActivity(intent); 
     } 
    } 
    private StringBuilder inputStreamToString(InputStream is) { 
     String rLine = ""; 
     StringBuilder answer = new StringBuilder(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     try { 
      while ((rLine = br.readLine()) != null) { 
       answer.append(rLine); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return answer; 
    } 
} 
private int returnParsedJsonObject(String result){ 

    JSONObject resultObject = null; 
    int returnedResult = 0; 
    try { 
     resultObject = new JSONObject(result); 
     returnedResult = resultObject.getInt("success"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return returnedResult; 
} 
} 

我使用WampServer與user="root"password=""

+0

你的問題在它現在的形式沒有多大意義。如果你有一個具體的問題,但你在博客文章或網站上閱讀的內容,你可能會想要在那裏發表評論。 – e4c5

+0

但我也有評論那裏先生,沒有迴應..同樣的問題引起其他用戶那裏...我只想知道serverUrl從哪裏來...我不知道路徑..請幫助我先生。 – Christina

+0

如果你仔細看,你會發現在你發佈的代碼中沒有任何對serverUrl的引用。 – e4c5

回答

1

「路徑」 可能是URL到您的本地Web服務器,即:

http://your_ip/your_path_to_your_php_script

+0

是index.php先生? – Christina

+0

先生,我得到這個錯誤:org.apache.http.conn.HttpHostConnectException:連接到http:// localhost拒絕 – Christina

+0

如果你在網絡上。運行ipconfig獲取你的IP地址。 在你的android應用程序中使用該ip從你的模擬器訪問你的網絡服務器。 您還需要在Web服務器中使用PHP登錄腳本來處理身份驗證。 – peter

1

感謝@ e4c5(對於建議)和@激光作爲答案。

我解決了!

因此,對於其他誰會偶然發現這個問題,檢查了這一點!^_^

轉到CMD >>鍵入ipconfig >>再看看IPv4地址 ...

然後將其粘貼在private final String serverUrl = "your path here"

http://192.168.1.8/android_user_api/index.php

謝謝更換your path here你們!

快樂編碼!

0

您需要找到您的計算機的IPV4地址,在您的計算機上運行命令提示符(轉到Windows啓動按鈕並啓動typig cmd)並鍵入ipconfig並輸入以查看您計算機的IPV4地址。

成功在您的計算機上運行WAMP服務器指示系統托盤上的綠色圖標,點擊該圖標,彈出菜單點擊放在線上項目。等待幾秒鐘來處理它的任務。

要在您的android studio模擬器中測試您的PHP MySQL應用程序,只需輸入地址http:\\IP Address\,然後在您的代碼中輸入PHP文件的路徑。

例如:http:\\192.168.1.7\test\example.php