使用凌空从Android应用程序的在线数据库中获取数组

时间:2017-04-02 11:37:44

标签: java php android database android-volley

首先,我必须告诉我,我对android studio& amp; PHP。

我设法通过登录,上传和搜索在线数据库来创建应用程序。

我用方法POST使用了凌空。

一切都很好&如果搜索结果是单一的,那就完美了。

问题是当有多个结果需要存储在数组中时。在这种情况下,我的代码只检索最后的结果。

我知道网上还有很多其他方法可供使用,但由于我是新手,我无法理解它们,因此根据我的数据进行编辑。

这是我的JAVA MainActivity代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

这是我的SearchRequest类代码:

package com.example.dell.myapplicationo;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText etname = (EditText) findViewById(R.id.etname);
    final TextView b = (TextView) findViewById(R.id.tvb);
    final Button c = (Button) findViewById(R.id.c);


    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String name = etname.getText().toString();


            // Response received from the server
            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {
                            String name = jsonResponse.getString("name");
                            String family = jsonResponse.getString("family");
                            String date= jsonResponse.getString("date");
                            String time= jsonResponse.getString("time");



                            // Display 
                            String message = name + family + date + time;
                            b.setText(message);


                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setMessage("Not Found")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            SearchRequest searchRequest = new SearchRequest(name, responseListener);
            RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
            queue.add(searchRequest);
        }
    });
}
}

最后这是我的PHP代码(search.php)

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class SearchRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://MYURL.hol.es/search.php";
private Map<String, String> params;

public SearchRequest(String name, Response.Listener<String> listener) {
    super(Method.POST, LOGIN_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("name", name);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

1 个答案:

答案 0 :(得分:0)

因为,在每次迭代之后,您将覆盖此前的值

 while(mysqli_stmt_fetch($statement)){
        $response["success"] = true; 
        $response["name"] = $name;
        $response["family"] = $family;
        $response["date"] = $date;
        $response["time"] = $time;

    }

您可以将以上代码更改为:

$response = array();
$arr = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true; 
    $response["name"] = $name;
    $response["family"] = $family;
    $response["date"] = $date;
    $response["time"] = $time;
    $arr[] = $response;
}
echo json_encode($arr);