简体   繁体   中英

Is there an easier, simple way to connect android studio to local host?

I'm trying to make a register request script. And as RiggsFolly mentioned before, I just keep repeating myself. So, I want to know how would I connect Android Studio to my localhost server? I was just told params might not be the solution, and I've seen other answers be way to complicated for this simple task.

It seems like params isn't sending data over properly . So I just want to know what should instead of params to send data to the php script. Now I've seen this script, and it looks to use an outside library that I couldn't import.

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());
    }

I couldn't import the http library, so I need to know if their is a in-built way to use http requests.

At the moment, my RegisterRequest script look like this;

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;
    }
}

Now, im using params.put to input the parameters into the php script, but the php doesn't seem to receive anything, and even spits out an undefined index error when I try to reference what it should be getting.

This is the php script. It WAS simple, but it just a mess of a code thats supposed to $_POST data into a "cresidentials" db. The rest of the code is just a hodge-podge of lines people wanted me to put in for debugging reasons.

<?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";
?>

The $_POST is spitting an undefined index error. And no, RiggsFolly, just because I said undefined index doesn't mean you can try to dupe this question to my other question. Stop that.

I do not believe is it required for me to put my CreateUser script in here, unless you want to change its params to jsonobjects. I would like to know anyone's simple way of just connecting android studio to php and posting data inside. And try not to go on a huge encryption deal, I'm very new at this. By the way, the output is this;

/System.out: ahhh /System.out: ahhhh /System.out: Admin /Response Value::
Notice: Undefined index: username in \\Register.php on line 6 NULL connection successfulvalues not set

🗅🗅🗅🗅🗅🗅I appreciate all help, thanks.🗅🗅🗅🗅🗅🗅

This is the part where Riggs edits my entire question

When you do this, you're sending a GET request without any parameters at all:

httpclient.execute(new HttpGet(URL));

Judging by your "request" class I'm guessing you're trying to use Volley. In Volley you have to create a RequestQueue and add your request to it. See the guide here: https://developer.android.com/training/volley/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM