Php TextView不是从json加载的

Php TextView不是从json加载的,php,android,mysql,json,Php,Android,Mysql,Json,我只是需要帮助,在这种情况下,我必须从远程服务器加载数据,并且我正在使用volley库从服务器获取值,所以这就是我到目前为止所做的 public class Leaderboard extends AppCompatActivity { private ListView leaderListView; private ProgressDialog loading; private TextView myname,myrank,myaccu; public static final String

我只是需要帮助,在这种情况下,我必须从远程服务器加载数据,并且我正在使用volley库从服务器获取值,所以这就是我到目前为止所做的

public class Leaderboard extends AppCompatActivity {
private ListView leaderListView;
private ProgressDialog loading;
private TextView myname,myrank,myaccu;
public static final String ROOT_URL = "http://thehostels.in/judgement_files/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.leaderboard);
    sendEmail();

    myname=(TextView)findViewById(R.id.myname);
    myrank=(TextView)findViewById(R.id.myrank);
    myaccu=(TextView)findViewById(R.id.myAccuracy);
    getMe();


}

 User user = SharedPrefManager.getInstance(Leaderboard.this).getUser();
 String userEmail= user.getEmail();

public void getMe(){


        loading = ProgressDialog.show(this,"","",false,false);
        String url = Config.Leaderboard.toString().trim();


        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        loading.dismiss();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

private void showJSON(String response){
    String textname="";
    String textrank="";
    String textacc="";
    String val1="";
    String val2="";


    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY_Leader);
        JSONObject TeamData = result.getJSONObject(0);
        textname = TeamData.getString(Config.myname);
        textrank = TeamData.getString(Config.myrank);
        textacc = TeamData.getString(Config.myaccuracy);
        val1=("#"+textrank+".");
        val2=(textacc+"%");

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

    myrank.setText(val1);
    myname.setText(textname);
    myaccu.setText(val2);
}

   public void sendEmail(){


    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(ROOT_URL) //Setting the Root URL
            .build(); //Finally building the adapter

    //Creating object for our interface
    myEmail api = adapter.create(myEmail.class);

    //Defining the method insertuser of our interface
    api.sendMail(

            userEmail,
            //Creating an anonymous callback
            new Callback<retrofit.client.Response>() {
                @Override
                public void success(retrofit.client.Response result, retrofit.client.Response response) {
                    //On success we will read the server's output using bufferedreader
                    //Creating a bufferedreader object
                    BufferedReader reader = null;

                    //An string to store output from the server
                    String output = "";

                    try {
                        //Initializing buffered reader
                        reader = new BufferedReader(new InputStreamReader(result.getBody().in()));

                        //Reading the output in the string
                        output = reader.readLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //Displaying the output as a toast
                    Toast.makeText(Leaderboard.this, output, Toast.LENGTH_LONG).show();


                }

                @Override
                public void failure(RetrofitError error) {
                    //If any error occured displaying the error as toast
                    Toast.makeText(Leaderboard.this, "something went wrong", Toast.LENGTH_LONG).show();
                }
            }
    );
}
myleadboard.php

<?php
  if($_SERVER['REQUEST_METHOD']=='POST'){
     $email=$_POST['userEmail'];
      define('HOST','xxxxxxxx');
      define('USER','xxxxxxxx');
      define('PASS','xxxxxxxx');
      define('DB','xxxxxxxx');

      $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

      $sql = "select * from Leaderboard where email='$email'";

      $res = mysqli_query($con,$sql);
      $result = array();
      while($row = mysqli_fetch_array($res)){
      array_push($result,
      array('name'=>$row[1],
      'rank'=>$row[2],
      'accuracy'=>$row[3]));
       }
      echo json_encode (array("list"=>$result));
        }
       mysqli_close($con);

        }
           ?>
包含文本视图的主文件 leadboard.xml

        <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="3sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<funplay.dreamstar1.CustomTextView
    android:layout_marginLeft="10sp"
    android:layout_marginStart="10sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="#1"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myrank"/>

<funplay.dreamstar1.CustomTextView
    android:layout_marginLeft="35sp"
    android:layout_marginStart="35sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="Vipul S"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myname"/>

<funplay.dreamstar1.CustomTextView
    android:layout_marginEnd="12sp"
    android:layout_marginRight="12sp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="100%"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myAccuracy"/>

问题是MySQL表包含很多列,我只想从该表中检索一些列,并用json编码,以将其返回到我的android代码中,但不知何故,它没有发生,数据没有在textview中填充,我已经调试了PHP代码,它工作正常


这里的
sendmail
函数用于将安卓系统的电子邮件发送到
myleadboard.php
文件,然后该文件只选择带有匹配电子邮件的行,因此我只想知道这里有什么问题,或者是否有其他方法……

下面给出的是您从php返回的json。(我对php不太熟悉,不建议您在php代码中出现任何错误。但因为您声称php代码工作正常,并且返回了您想要/期望的json。)

创建以下两个类

public class MyPhpResponse {
    private List<MyPhpObject> list;

    public List<MyPhpObject> getList() {
        return list;
    }

    public void setList(List<MyPhpObject> inputList) {
        list = inputList;
    }
}

public class MyPhpObject {
    private String name;
    private String rank;
    private String accuracy;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(String accuracy) {
        this.accuracy = accuracy;
    }
}
这会暴露JSON的任何问题,如果没有问题,TextView肯定会显示数据。
让我知道它是如何为您工作的。

只是为了清楚地了解情况。你看不到这三种说法的结果,对吗<代码>myrank.setText(val1);myname.setText(textname);myaccu.setText(val2)。在将
val1、val2
textname
设置为这些文本视图之前,您是否通过调试确保已填充
val1、val2
textname
?当然可以……您是否可以在存在listview和textView的位置共享xml文件内容?我已经添加了xml文件,在将变量初始化为TextView时,xml具有funplay.dreamstar1.CustomTextView。CustomTextView在本期中非常重要,这是您应该首先提及并提供代码的内容。:)你确定你的CustomTextView代码不是罪魁祸首吗?因为我们不知道类中发生了什么。是的,json实际上是不对的,主要问题是php文件的email变量是动态变化的,所以我需要类似于这个答案的任何东西,我想这就是我想要的,更适合我的情况……你知道我的情况,所以我请求你在我的情况下指导我回答这个问题。我试图指导你,但我需要你方面的适当解释,以了解确切的问题是什么。您提到JSON是正确的,问题在于TextView,现在我们发现JSON不正确。您没有从android上的调试器中获取问题中的JSON吗?这正是我所期望的。这看起来不错。您是否从php发送给android的响应中获取json?或者它只是您所期望的json模板。
        <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="3sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<funplay.dreamstar1.CustomTextView
    android:layout_marginLeft="10sp"
    android:layout_marginStart="10sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="#1"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myrank"/>

<funplay.dreamstar1.CustomTextView
    android:layout_marginLeft="35sp"
    android:layout_marginStart="35sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="Vipul S"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myname"/>

<funplay.dreamstar1.CustomTextView
    android:layout_marginEnd="12sp"
    android:layout_marginRight="12sp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="100%"
    android:layout_marginTop="20sp"
    android:textSize="17sp"
    android:id="@+id/myAccuracy"/>
{
    "list": [
        {
            "name": "Vipul S",
            "rank": "1",
            "accuracy": "80"
        }
    ]
}
public class MyPhpResponse {
    private List<MyPhpObject> list;

    public List<MyPhpObject> getList() {
        return list;
    }

    public void setList(List<MyPhpObject> inputList) {
        list = inputList;
    }
}

public class MyPhpObject {
    private String name;
    private String rank;
    private String accuracy;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(String accuracy) {
        this.accuracy = accuracy;
    }
}
private void showJSON(String response) {
        try {
            final Gson gson = new Gson();
            final MyPhpResponse myPhpResponse = gson.fromJson(response, MyPhpResponse.class);
            //Assuming you are getting only one object back in the list
            final MyPhpObject myPhpObject = myPhpResponse.getList().get(0);
            myrank.setText(myPhpObject.getRank());
            myname.setText(myPhpObject.getName());
            myaccu.setText(myPhpObject.getAccuracy());
        } catch (MalformedJsonException e) {
            e.printStackTrace();
        }
    }