[英]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.