【问题标题】:volley post arrayrequest not working凌空后阵列请求不起作用
【发布时间】:2017-12-19 01:17:58
【问题描述】:

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

它没有跳转到@overwrite 响应。

希望有人能帮助我。

我已经找了好几天了,为什么它不起作用

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,但我无法弄清楚邮递员的工作原理
    • 使用这个firefox插件addons.mozilla.org/en-US/firefox/addon/restclient比邮递员简单
    • 你看到我调试答案了吗?也许你会变得更聪明;-)
    • @Jusi 你的意思是他的 php 后端没问题?也许他也有数据库错误。为什么不向 OP 和我们展示如何调试,这样我们才能变得更聪明!
    【解决方案2】:

    在我看来你应该首先使用像postman这样的api测试软件。 postman 非常适合。 这也对我有用

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

    【讨论】:

    • 我如何在 volley 库的 jsonarray 请求中使用 post 方法。所以它对我的 php 脚本产生了干扰。因为只有 jsonstring 请求支持您可以使用的 post 方法:@Override public Map getParams() { return params; }
    • 在 volley 请求中定义它是字符串请求中的 post 请求,如 StringRequest str= new StringRequest (Request.Method.POST
    • 看下面的答案
    【解决方案3】:

    我确实喜欢以下内容:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多