Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用volley从android应用程序的在线数据库获取数组_Java_Php_Android_Database_Android Volley - Fatal编程技术网

Java 使用volley从android应用程序的在线数据库获取数组

Java 使用volley从android应用程序的在线数据库获取数组,java,php,android,database,android-volley,Java,Php,Android,Database,Android Volley,首先,我必须告诉大家,我对androidstudio&PHP相当陌生 我已经设法使登录,上传和搜索在线数据库的应用程序 我用了截击的方法 如果搜索结果是单一的,那么一切都很好 问题是当一个数组中需要存储多个结果时。在本例中,我的代码仅检索最后一个结果 我知道网上还有很多其他的方法,但由于我是新手,我无法理解它们,因此无法根据我的数据对它们进行编辑 以下是我的JAVA MainActivity代码: package com.example.dell.myapplicationo; import

首先,我必须告诉大家,我对androidstudio&PHP相当陌生

我已经设法使登录,上传和搜索在线数据库的应用程序

我用了截击的方法

如果搜索结果是单一的,那么一切都很好

问题是当一个数组中需要存储多个结果时。在本例中,我的代码仅检索最后一个结果

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

以下是我的JAVA MainActivity代码:

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);
        }
    });
}
}
package com.example.dell.myapplicationo;
导入android.app.AlertDialog;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终EditText etname=(EditText)findViewById(R.id.etname);
最终文本视图b=(文本视图)findViewById(R.id.tvb);
最终按钮c=(按钮)findViewById(R.id.c);
c、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
最终字符串名称=etname.getText().toString();
//从服务器收到的响应
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功){
String name=jsonResponse.getString(“name”);
String family=jsonResponse.getString(“family”);
String date=jsonResponse.getString(“日期”);
String time=jsonResponse.getString(“time”);
//展示
字符串消息=姓名+家庭+日期+时间;
b、 setText(信息);
}否则{
AlertDialog.Builder=新建AlertDialog.Builder(MainActivity.this);
builder.setMessage(“未找到”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
SearchRequest SearchRequest=新的SearchRequest(名称、响应侦听器);
RequestQueue=Volley.newRequestQueue(MainActivity.this);
添加(searchRequest);
}
});
}
}
这是我的SearchRequest类代码:

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;
}
}
import com.android.volley.Response;
导入com.android.volley.toolbox.StringRequest;
导入java.lang.reflect.Method;
导入java.util.HashMap;
导入java.util.Map;
公共类SearchRequest扩展了StringRequest{
私有静态最终字符串登录\u请求\u URL=”http://MYURL.hol.es/search.php";
私有映射参数;
publicsearchRequest(字符串名,Response.Listener){
super(Method.POST,LOGIN\u REQUEST\u URL,listener,null);
params=新的HashMap();
参数put(“名称”,名称);
}
@凌驾
公共映射getParams(){
返回参数;
}
}
最后是我的php代码(search.php)


因为在每次迭代之后,您都会在此处覆盖上一个值

 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);

谢谢事实上,我知道这个信息不是我的核心问题。我本可以用以下方法代替:while($row=mysqli\u fetch\u array($query)),但我也不知道如何用JAVA接收它,如果您能告诉我如何用JAVA解码,我将非常感谢。JAVA的更改取决于您的json编码。
$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);