android php接口 studio_使用Android Studio和PHP的Android到Wamp服务器连...

我是新手.我想将我的Android应用程序连接到wampserver数据库.我在此link上找到了一个教程,但是在wampserver中应该在哪里找到我的serverUrl?它看起来应该像这样:private final String serverUrl =“您的路径在这里”那么我在哪里可以找到“您的路径在这里”?

这是我的代码:

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 {

@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 nameValuePairs = new ArrayList(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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值