有没有一种更简单的方法将android工作室连接到本地主机



我正在尝试制作一个注册请求脚本。正如RiggsFilly之前提到的,我只是不断重复自己。所以,我想知道如何将Android Studio连接到本地主机服务器?我刚刚被告知params可能不是解决方案,我看到其他答案对这个简单的任务来说太复杂了

看起来params没有正确发送数据。所以我只想知道应该用什么而不是params来向php脚本发送数据。现在我看到了这个脚本,它看起来使用了一个我无法导入的外部库。

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
String responseString = out.toString();
out.close();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}

我无法导入http库,所以我需要知道它们是否是使用http请求的内置方式。

目前,我的RegisterRequest脚本是这样的;

public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
private Map<String, String> params;
public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL,listener,null);
System.out.println(username);
System.out.println(password);
System.out.println(isAdmin);
params = new HashMap<>();
params.put("username",username);
params.put("password",password);
params.put("isAdmin",isAdmin+"");
}
public Map<String, String> getparams() {
return params;
}
}

现在,我使用params.put将参数输入到php脚本中,但php似乎没有接收到任何内容,甚至在我试图引用它应该得到的内容时抛出了一个未定义的索引错误。

这是php脚本。这很简单,但它只是一个混乱的代码,应该将数据$_POST到"creidentials"数据库中。其余的代码只是人们出于调试原因希望我加入的一大堆行。

<?php
$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
var_dump($_POST["username"]);//line 6
$con = mysqli_connect($db_host,'root',$db_pass,$db_name);
if($con){
echo "connection successful";
}
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}
if(isset($_POST["isAdmin"]) &&  isset($_POST["username"]) && isset($_POST["password"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$isAdmin = $_POST["isAdmin"];
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement,'sss',$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(!$statement) 
{
printf("Prepare failed: %sn", mysqli_error($con)); 
}
echo "success";
}
else
echo "values not set";
?>

$_POST正在抛出一个未定义的索引错误。不,RiggsFilly,仅仅因为我说了未定义索引并不意味着你可以把这个问题骗到我的另一个问题上。别这样。

我不认为我需要把CreateUser脚本放在这里,除非你想把它的params改为jsonobjects。我想知道任何人只需将android工作室连接到php并在里面发布数据的简单方法。尽量不要进行大规模的加密交易,我对此很陌生。顺便说一下,输出是这样的;

/System.out:ahh/System-out:ahhh/System.out:Admin/响应值::
注意:未定义的索引:中的用户名\第6行的Register.phpNULL连接成功值未设置

我感谢所有的帮助,谢谢。

这是里格斯编辑我整个问题的部分

当你这样做时,你发送的是一个没有任何参数的GET请求:

httpclient.execute(new HttpGet(URL));

从你的"请求"类判断,我猜你是在试图使用Volley。在Volley中,您必须创建一个RequestQueue并将您的请求添加到其中。请参阅此处的指南:https://developer.android.com/training/volley/

最新更新