2015-09-05 12:57
浏览 125

Android使用Android Studio和PHP进行Wamp Server连接

I'm newbie here. I want to connect my android app to wampserver database. I found a tutorial on this link, But where should I find my serverUrl in wampserver? It should look like this: private final String serverUrl = "your path here" So where do I find "your path here"?

Here is my code:


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
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.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";

    protected void onCreate(Bundle savedInstanceState) {

        username = (EditText)findViewById(;
        password = (EditText)findViewById(;
        Button loginButton = (Button)findViewById(;
        Button registerButton = (Button)findViewById(;

        loginButton.setOnClickListener(new View.OnClickListener() {
            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();
                if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){
                    Toast.makeText(MainActivity.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show();

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


        registerButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,         RegisterActivity.class);
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
    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 == {
        return true;

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

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

I am using WampServer with user="root" and password=""

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyangzhi0687 2015-09-05 13:22

    The "path" is probably the URL to your local web server, that is:


    打赏 评论
  • douweng7308 2015-09-05 15:43

    Thanks to @e4c5 (for the suggestions) and to @Laser for the answer(s).

    I solved it!

    So for the others who will stumble upon this problem, check this out! ^_^

    Go to cmd >> type ipconfig >> then look at IPv4 Address...

    Then paste it on private final String serverUrl = "your path here"

    Replace your path here with

    Thank you guys!

    Happy Coding!

    打赏 评论
  • duanqiao2225 2016-06-18 16:21

    you need to find your IPV4 address of your computer, run command prompt in your computer(go to Windows start button and start typig cmd) and type ipconfig and enter to see your IPV4 address of your computer.

    Successfully Running WAMP server on your computer indicates a green icon on system tray, click on that, with a menu pop up click on put online item. Wait for few second to process its task.

    To test your app for PHP MySQL in your android studio emulator, just enter address as http:\\IP Address\ followed by path of your PHP file in your code.

    For example: http:\\\test\example.php

    打赏 评论

相关推荐 更多相似问题