JSON到Android的getString

时间:2017-09-19 02:01:22

标签: php android json mysqli

我目前正在官员姓名,但我想添加参考,但我不知道我是否正确执行了该程序以及如何在android上获取参考值?目前我在应用程序活动开始时敬酒官员姓名。我想添加对吐司的引用,但我不知道如何获得它的价值。

php脚本

<?php

require "init.php";
$user_name = $_POST["user_name"];
$password = $_POST["password"];
$activate;
$sql = "select officer_name,reference from user_tbl where user_name like '" . $user_name . "' and password like '" . $password . "' and activate = 1;";

$result = mysqli_query($con, $sql);
$response = array();
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_row($result);
    $officer_name = $row[0];
    $reference = $row[1];
    $code = "Login_Success";
    array_push($response, array("code" => $code,
        "officer_name" => $officer_name,
        "reference" => $reference));
    echo json_encode($response);
} else {
    $code = "Login_Failed";
    $message = "Error! User not found or Not Activated";
    array_push($response, array("code" => $code, "message" => $message));
    echo json_encode($response);

}

mysqli_close($con);

?>

安卓代码

 StringRequest stringRequest = new StringRequest(Request.Method.POST, loginUrl,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONArray jsonArray = new JSONArray(response);
                                    JSONObject jsonObject = jsonArray.getJSONObject(0);

                                    String code = jsonObject.getString("code");
                                    if (code.equals("Login_Failed"))
                                    {
                                        builder.setTitle("Login Error");
                                        displayAlert(jsonObject.getString("message"));
                                    }
                                    else
                                    {
                                        Intent intent = new Intent(Login.this,MainActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putString("officer_name", jsonObject.getString("officer_name"));
                                        intent.putExtras(bundle);
                                        startActivity(intent);

1 个答案:

答案 0 :(得分:0)

使用 jsonObject.getString 方法,例如<modelVersion>4.0.0</modelVersion> <groupId>com.xxx.xxx</groupId> <artifactId>xxx</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>xxx</name> <build> <finalName>xxx</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- jersey --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <!-- JSON support --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> <!-- postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.1.4</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.5</version> </dependency> <!-- logging for hibernate --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <!-- password hashing --> <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.3m</version> </dependency> <!-- http client --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.0</version> </dependency> </dependencies> <properties> <jersey.version>2.25</jersey.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

请参阅this for reference