我的安卓应用程序没有得到来自服务器的任何响应



我是Android的初学者。实际上,我正在尝试从服务器接收数据并检查输入的用户名和密码是否正确,但是它没有收到任何显示"错误匹配"的响应事件。我还检查了 JSON 输出,如果输入"Hello"作为名称和"Hi"作为密码,它应该显示成功,但它在单击按钮时没有显示任何内容。我还在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"}]}

日志中的错误:

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)

所需连接.php文件中必须有"已建立连接"消息。

这就是为什么 php 响应不是有效的 json 的原因。

相关内容

最新更新