I just need help with a situation where i have to load data from remote server and i am using volley library for the purpose of fetching the values from the server so this is what i have done so far
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();
}
}
);
}
and config.java file
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);
}
?>
my json returns me
{"list":[{"name":"Vipul S","rank":"1","accuracy":"80"}]}
main file which contains 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"/>
so the thing is MySQL table contains many columns and i just wanna retrieve few columns from that table and encode in json to get it back to my android code but somehow it is not happening and data is not populating in textview and i have debug the PHP code its working all fine.
here sendEmail
function is used to send email from android to myleaderboard.php
file which then only chooses the row with matching email so i just wanna know what is problem over here or is there any alternative way to do so....