gpt4 book ai didi

php - 如何使用 volley api 将 android 应用程序连接到本地服务器

转载 作者:行者123 更新时间:2023-11-29 15:41:41 26 4
gpt4 key购买 nike

我正在开发一个需要使用 volley 库登录和注册的应用程序。

测试应用程序我输入了我的凭据并注册了,但它没有将我的详细信息发布到我的本地服务器,我在应用程序中收到了一条 toast 消息 “com.android.volley.servererror”

这是我的 volleyRegister.php 文件:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
require_once('dbConnect.php');
$sql = "INSERT INTO android-volley (fullname,email,username,password) VALUES ('$fullname','$email','$username','$password')";
if(mysqli_query($con,$sql)){
echo "Successfully Registered";
}else{
echo "Could not register";
}}else{
echo 'error';}?>

安卓代码

public class SignUpFragment extends Fragment implements Button.OnClickListener {


public static final String Register_URL ="http://192.168.0.107/android-connect/volleyRegister.php";



public static final String KEY_FULLNAME = "fullname";
public static final String KEY_EMAIL = "email";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";



EditText etFullName;
EditText etEmail;
EditText etUsername;
EditText etPassword;
Button SignUp2;


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_signup, container, false);

final Animation animAlpha = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_alpha);

etFullName = (EditText) rootView.findViewById(R.id.etFullName);
etEmail = (EditText) rootView.findViewById(R.id.etEmail);
etUsername = (EditText) rootView.findViewById(R.id.etUsername);
etPassword = (EditText) rootView.findViewById(R.id.etPassword);
Button SignUp2 = (Button) rootView.findViewById(R.id.Signup2);


SignUp2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final String fullName = etFullName.getText().toString();
final String email = etEmail.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString().trim();

AnimationSet sets = new AnimationSet(false);
sets.addAnimation(animAlpha);
v.startAnimation(sets);

if (etFullName.length() == 0 || etEmail.length() == 0 || etUsername.length() == 0 || etPassword.length() == 0) {
Toast.makeText(getActivity(), "Oops! fill in complete details", Toast.LENGTH_SHORT).show();


} else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, Register_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Toast.makeText(SignUpFragment.this.getActivity(), s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(SignUpFragment.this.getActivity(), volleyError.toString(), Toast.LENGTH_LONG).show();
}


}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(KEY_FULLNAME, fullName);
params.put(KEY_EMAIL, email);
params.put(KEY_USERNAME, username);
params.put(KEY_PASSWORD, password);
return params;
}


};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
}});

return rootView;
}

`

最佳答案

在你的 php 系统(pc 或 lappy)中打开 cmd 并输入 ipconfig

复制你的IP地址

像这样使用它

//测试ip 192.168.0.1

private void callvolly() {
String url = "http://192.168.0.1/thenyourfolderpath";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {


//responce

}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {


}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("", "");//params
Log.e("send", params.toString());
return params;
}

};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
20000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

}

关于php - 如何使用 volley api 将 android 应用程序连接到本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com