我只需要帮助处理我必须从远程服务器加载数据的情况,并且我正在使用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();
}
}
);
}
和config.java文件
public class Config {
public static final String Leaderboard = "http://thehostels.in/judgement_files/myleaderboard.php";
public static final String myname = "name";
public static final String myrank = "rank";
public static final String myaccuracy = "accuracy";
public static final String JSON_ARRAY_Leader= "list";
}
myleaderboard.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);
}
?>
我的json回报我
{"list":[{"name":"Vipul S","rank":"1","accuracy":"80"}]}
包含textviews的主文件 leaderboard.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代码,它的工作正常。
这里sendEmail
函数用于将电子邮件从android发送到myleaderboard.php
文件,然后只选择匹配电子邮件的行,所以我只想知道这里有什么问题,或者有没有其他方法可以这样做....
答案 0 :(得分:0)
下面给出的是你从php回来的json。 (我不太熟悉php来建议你的PHP代码中的任何错误。但是因为你声称php代码工作正常并且返回你想要/期望的json。)
{
"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;
}
}
现在使用Google的Gson库https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.2并在showJSON方法中执行以下操作:
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();
}
}
这应该暴露JSON的任何问题,如果没有问题,TextView应该绝对显示数据。 让我知道它是如何为你工作的。