我是Android的初学者。实际上我试图从服务器接收数据并检查输入的用户名和密码是否正确,但是没有得到任何响应事件显示"错误匹配"。我还检查了JSON输出,如果输入的话,它会显示成功" Hello"作为名称和"嗨"作为密码,但它没有在按钮点击上显示任何内容。我还在 Manifest.xml
中添加了互联网权限。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String REGISTER_URL = "http://192.168.43.218/define_testing.php";
EditText username,pass;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
username=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
login.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest request=new StringRequest(REGISTER_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsO= new JSONObject(response);
JSONArray array = null;
array = jsO.getJSONArray("result");
JSONObject obj = array.getJSONObject(0);
String name= obj.getString("name");
String password=obj.getString("password");
String enterName=username.getText().toString();
String enterPass=pass.getText().toString();
if(name.equals(enterName) && password.equals(enterPass)){
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),"Wrong Match",Toast.LENGTH_SHORT).show();
}
}
catch (JSONException e1) {
e1.printStackTrace();
}
}},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Internet Problem",Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
requestQueue.add(request);
}
}
);
}
}
PHP脚本
<?php
require ('connection.php');
$name = 'Hello';
$sql = "SELECT * FROM new_one_user_information WHERE username='".$name."'";
$r = mysqli_query($connect,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"name"=>$res['username'],
"password"=>$res['password']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($connect);
?>
此PHP脚本的输出是:
Connection Established
{"result":[{"name":"Hello","password":"Hi"}]}
logcat错误:
org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:172)
at com.example.srinu.stackview2.MainActivity.checkIt(MainActivity.java:85)
at com.example.srinu.stackview2.MainActivity$1$1.onResponse(MainActivity.java:68)
at com.example.srinu.stackview2.MainActivity$1$1.onResponse(MainActivity.java:50)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
必需的connection.php文件中必须有“建立连接”消息。
这就是为什么php响应不是有效的json。