0

The program gets input rollno and name and displays the address and marks based on it. I get the output for marks and address as null instead of the value stored in the databse. The PHP code works fine. >

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
private EditText editText2; 
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
buttonGet = (Button) findViewById(R.id.buttonGet);
textViewResult = (TextView) findViewById(R.id.textViewResult);
buttonGet.setOnClickListener(this);
}

private void getData() {
String rollno = editText.getText().toString().trim();
if (rollno.equals("")) {
Toast.makeText(this, "Please enter an rollno", Toast.LENGTH_LONG).show();
            return;
}
String name = editText2.getText().toString().trim();
if (name.equals("")) {
Toast.makeText(this, "Please enter an name", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = http://192.14.6.113/getData.php?rollno="+rollno+" & name="+name;
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) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
 RequestQueue requestQueue = Volley.newRequestQueue(this);
 requestQueue.add(stringRequest);
}
 private void showJSON(String response){
tring address="";
String marks = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
address = collegeData.getString(Config.KEY_ADDRESS);
marks = collegeData.getString(Config.KEY_MARKS);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Address:\t" +address+ "\nMarks:\t"+ marks);
}
 @Override
public void onClick(View v) {
getData();
    }
}
Stephen
  • 9,899
  • 16
  • 90
  • 137

0 Answers0