凌空射击阵列请求无法正常工作

时间:2017-12-19 01:17:58

标签: java php arrays http-post android-volley

我尝试创建代码以从我的服务器获取所有用户,除了我。

它没有跳转到@overwrite响应。

我希望sombody可以帮助我。

我现在正在寻找为什么它不能正常工作

XD没有找到问题。

我的PHP代码:

<?php
    $con = mysqli_connect("localhost", "userID", "password", "database");

    $name = $_POST["name"];
    $passwort = $_POST["passwort"];

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE name = ? AND passwort = ?");
    mysqli_stmt_bind_param($statement, "ss", $name, $passwort);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $userID, $name, $vorname, $nachname, $passwort);

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

    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
        $response["name"] = $name;
        $response["vorname"] = $vorname;
        $response["nachname"] = $nachname;
        $response["passwort"] = $passwort;
    }

    echo json_encode($response);
?>

在JAVA中,他们都聚集在一起:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Adapter;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.android.app.Data.LoginRequest;
import com.example.android.app.Data.RegistryRequest;
import com.example.android.app.Data.UserlistRequest;
import com.example.android.app.Tools.User;
import com.example.android.app.Tools.UserAdapter;

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

import java.util.ArrayList;
import java.util.Map;

public class listeAndererActivity extends AppCompatActivity {
    private String Tag="lsiteanderer";
    private ArrayList<User> userListe = new ArrayList<>();
    private UserAdapter useradapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liste_anderer);
        String ownname="juli";

        /////VERSION 2///////////////////////////////////////////////////////////////

        Response.Listener<JSONArray> responseListenerJSONArray=new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject obj = response.getJSONObject (i);
                        User user = new User (obj.getString ("name"),obj.getInt("user_id"));

                        Log.v(Tag, "name in der for schlife ist"+user.getName());
                        userListe.add(user);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        useradapter=new UserAdapter(this, userListe);

        UserlistRequest userlistRequest =  new UserlistRequest(ownname, responseListenerJSONArray);
        RequestQueue queue= Volley.newRequestQueue(listeAndererActivity.this);
        queue.add(userlistRequest);

        ListView listView = (ListView) findViewById(R.id.AndererListe);

        listView.setAdapter(useradapter);
    }
}

我的要求等级:

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;

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

/**
 * Created by Juli on 17.12.2017.
 */

public class UserlistRequest extends JsonArrayRequest {

    private static final String userlistRequestUrl="https://unityentertainment.000webhostapp.com/OtherUser.php";
    private Map<String,String> params;


    public  UserlistRequest(String name, Response.Listener<JSONArray> listener){
        super(Request.Method.POST, userlistRequestUrl, listener, null);

        params= new HashMap<>();
        params.put("name", name);
    }

   // public UserlistRequest(String ownname, Listener<JSONArray> responseListenerJSONArray) {
     //   super();
   // }

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

我的数据类:

public class User {
    private String name;
    private int user_id;

    public User(String name, int user_id){
        this.name=name;
        this.user_id=user_id;
    }

    public String getName() {
        return name;
    }

    public int getUser_id() {
        return user_id;
    }
}

我的适配器:

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.example.android.app.R;
import com.example.android.app.ListeAndererActivity;

import java.util.ArrayList;

public class UserAdapter extends ArrayAdapter<User> {
    public UserAdapter(ListeAndererActivity activity, ArrayList<User> userArrayList) {
        super((Context) activity, 0 ,userArrayList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.itemlist_user, parent, false);
        }

        // Get the {@link AndroidFlavor} object located at this position in the list
        User currentAndroidFlavor = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID version_name
        TextView nameTextView = (TextView) listItemView.findViewById(R.id.itemlistUser_TextView);
        // Get the version name from the current AndroidFlavor object and
        // set this text on the name TextView
        nameTextView.setText(currentAndroidFlavor.getName());

        return listItemView;
    }
}

3 个答案:

答案 0 :(得分:0)

您应该跟踪您的请求和响应,并查看它停止的位置。使用像邮递员或一些firefox / chrome插件这样的东西来测试代码的后端php rest API部分并缩小可能性,并通过打印值监视测试设备日志,并查看代码停止或行为与预期不同的位置。

答案 1 :(得分:0)

从我的观点来看,你应该首先使用api测试软件,如postmanpostman非常棒。 这对我有用

$stmt=$db->prepare("SELECT * FROM search WHERE designation LIKE ?");
    $stmt->bind_param("s",$a);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;
}
//you could then do something like this
$response['test']=$result;
echo json_encode($result);

答案 2 :(得分:0)

我确实喜欢以下内容:

public class UserlistRequest extends JsonArrayRequest {

    private static final String userlistRequestUrl="https://unityentertainment.000webhostapp.com/OtherUser.php";
    private Map<String,String> params;

    public UserlistRequest(String name, Response.Listener<JSONArray> listener) {
        super(Method.POST, userlistRequestUrl, (String) null, listener, null);



    // public  UserlistRequest(String name, Response.Listener<JSONArray> listener){
     //   super(Request.Method.POST, userlistRequestUrl, listener, null);


    params= new HashMap<>();
        params.put("name", name);
        params.put("name", name);

}

   // public UserlistRequest(String ownname, Listener<JSONArray> responseListenerJSONArray) {
     //   super();
   // }

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

但它没有使用getparams