![](/img/trans.png)
[英]In android studio StringRequest can connect to internet but not to local host with ip address?
[英]Is there an easier, simple way to connect android studio to local host?
我正在尝试制作注册请求脚本。 正如RiggsFolly之前提到的,我一直在重复自己。 因此, 我想知道如何将Android Studio连接到我的本地主机服务器? 有人告诉我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数据存入“ residentials”数据库。 其余的代码只是人们出于调试原因而希望我加入的几行代码。
<?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: %s\n", 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: %s\n", mysqli_error($con));
}
echo "success";
}
else
echo "values not set";
?>
$ _POST正在吐出未定义的索引错误。 不,RiggsFolly,只是因为我说未定义的索引并不意味着您可以尝试将此问题重复到另一个问题。 别搞了。
我不认为我需要将我的CreateUser脚本放在这里,除非您要将其参数更改为jsonobjects。 我想知道任何人将android studio连接到php并在其中发布数据的简单方法。 并且不要尝试进行大规模的加密交易,我对此非常陌生。 顺便说一句,输出是这个;
/System.out:ahhh /System.out:ahhhh /System.out:管理员/响应值::
注意:未定义的索引:第6行\\ Register.php中的用户名未设置NULL连接成功值
appreciate我感谢所有帮助,谢谢🗅🗅🗅🗅🗅🗅
这是Riggs编辑我的整个问题的部分
执行此操作时,您将发送一个不带任何参数的GET请求:
httpclient.execute(new HttpGet(URL));
从您的“请求”类来看,我猜您正在尝试使用Volley。 在Volley中,您必须创建一个RequestQueue并将您的请求添加到其中。 请参阅以下指南: https : //developer.android.com/training/volley/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.