[英]My android app didnt receive any response from server while sending json
[英]My Android app is not getting any response from server
我是Android的初学者。 实际上,我正在尝试从服务器接收数据并检查输入的用户名和密码是否正确,但是没有收到任何显示“错误匹配”的响应事件。 我还检查了JSON输出,如果输入“ Hello”作为名称并输入“ Hi”作为密码,则假设显示成功,但是单击按钮时未显示任何内容。 我还在Manifest.xml
添加了Internet权限。
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)
必需的connection.php文件内必须有“已建立连接”消息。
这就是为什么php响应不是有效的json的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.