I am very new in android.In my app I want to set retrieved data from mysql database in different textview.I tried many way but I am unable to do it.Always get a error.I also tried that.Please help me.I Want to set that data in textview.
I call this class from onCreate method
/*ERROR LINE*/private class GetData extends AsyncTask<String, String, String> {
public Context context;
public GetData(Context context) {
}
@Override
protected String doInBackground(String... params) {
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://www.mybusket.com/webapi/carrental/car/get_car_details.php?vcarid="+Id); //this Id is a int variable
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject obj = new JSONObject(str);
JSONArray jsonArray = obj.getJSONArray("car");
JSONObject json=jsonArray.getJSONObject(0);
brand.setText(json.getString("brand"));//ERROR LINE
fuel.setText(json.getString("fueltype"));
carno.setText(json.getString("carno"));
seat.setText(json.getString("seat"));
feature.setText(json.getString("feature"));
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
ERROR:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.arpan.carrental, PID: 15029 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.arpan.carrental.CarView$1.onResponse(CarView.java:130) at com.example.arpan.carrental.CarView$1.onResponse(CarView.java:115)
This is my php
<?php
$response = array();
// include db connect class
require_once("../connectdb.php");
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["carid"])) {
$carid = $_GET['carid'];
// check for post data
if (isset($_GET["carid"])) {
$carid = $_GET['carid'];
$result = mysql_query("SELECT * FROM carinfo WHERE carid = $carid");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$car = array();
$car["brand"] = $row["brand"];
$car["fueltype"] = $row["fueltype"];
$car["carno"] = $row["carno"];
$car["seat"] = $row["seat"];
$car["feature"] = $row["feature"];
// success
$response["success"] = 1;
// user node
$response["car"] = array();
array_push($response["car"], $car);
// echoing JSON response
echo json_encode($response);
} else {
// no employer found
$response["success"] = 0;
$response["message"] = "No Car found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no employer found
$response["success"] = 0;
$response["message"] = "No Car found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
I want all data comes from database and display in different textview.