虽然给出了很多资源和链接,但它们似乎都不适合我。我有一个返回JSON的PHP代码。我想在我的Android应用程序中从中提取值。这里是php代码。
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$username1 = $_POST['username'];
$password1= $_POST['password'];
require_once'config.php';
$sql = "SELECT * FROM users";
$result1 = $conn->query($sql);
$stat= 1;
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$user=$row1['username'];
$pass=$row1['password'];
if($user == $username1 && $pass == $password1){
$arr = array("stat" => "Valid","resp"=>"Logged in");
echo json_encode($arr);
$stat = 0;
break;
}
}
}
if ($stat == 1) {
echo "Username or Password is wrong!";
}
}
else{
echo 'Error in method';
}
?>
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String REGISTER_URL = "http://10.22.20.129/alak/login.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = (EditText) findViewById(R.id.TFusername);
editTextPassword = (EditText) findViewById(R.id.TFpassword);
buttonRegister = (Button) findViewById(R.id.Blogin);
buttonRegister.setOnClickListener(this);
}
private void registerUser(){
final String username = editTextUsername.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response.toString(), Toast.LENGTH_LONG).show();
try {
JSONArray json = new JSONArray(response.toString());
String x=(String) json.getString("resp");
if (x == "Logging") {
Intent in=new Intent(MainActivity.this,SignUp.class);
startActivity(in);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username",username);
params.put("password",password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
if(v == buttonRegister){
registerUser();
}
}
}
答案 0 :(得分:0)
我用过这个并找到了解决方案。谢谢
JSONObject obj = null;
try {
obj = new JSONObject(response.toString());
String stat = obj.getString("stat");
Toast.makeText(MainActivity.this,stat.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
对我来说最简单的方法是使用Gson进行空投 https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html