dongxing1965 2017-12-19 01:17
浏览 51

凌空后阵列请求不起作用

I tried to create code to get all users from my server except me.

It is not jumping to the @overwrite response.

I hope sombody can help me.

I was looking now for days why it's not working

XD didnt found the prob.

My php code:

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

In JAVA they all come together:

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

My Reqest Class:

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

My Data Class:

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

My Adapter:

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条回答 默认 最新

  • dqk77945 2017-12-19 02:13
    关注

    you should track your request and response and see where it stops. Use somthing like postman or some firefox/chrome plugin to test the back end php rest API part of your code and narrow down the possibilities and also monitor the testing device log by printing values and look where your code is stopping or behave different than intended.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考