Witam chciałem zrobić aplikacje w android studio która będzie pobierała dane z MySQL za pomocą JSon, skopiowałem kod ze strony https://abhishekprogramming.blogspot.in/2017/07/json-fetching-and-parsing-android-studio.html i próbowałem go przerobić na swoje potrzeby, niestety nie mogę pobrać danych, dane powinny pokazać się na label niestety tak się nie dzieje, dodam jeszcze ze kiedy próbowałem odpalić kod ściągnięty ze strony wszystko działało prawidłowo. Proszę pomóżcie bo utknąłem i nie mogę posunąć się dalej. Poniżej zamieszczam kod źródłowy
MainActivity
public class MainActivity extends AppCompatActivity {
Button click;
public static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.btnClick);
data = (TextView) findViewById(R.id.tvFetchData);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchData process = new fetchData();
process.execute();
}
});
}
}
fetchData
class fetchData extends AsyncTask<Void,Void,Void>{
String data ="";
String dataParsed = "";
String singleParsed ="";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("moj link");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}
JSONArray JA = new JSONArray(data);
for(int i =0 ;i <JA.length(); i++){
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "car:" + JO.get("transporter_car_plates") + "\n"+
"name:" + JO.get("name") + "\n"+
"code:" + JO.get("code") + "\n"+
"description:" + JO.get("description") + "\n";
dataParsed = dataParsed + singleParsed +"\n" ;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(this.dataParsed);
}
}
index.php
require_once 'Login_model.php';
$Login_model = new Login_model();
$Login_model->fetchData();
$response = array();
$response["transporter_car_plates"] = $Login_model->transporter_car_plates;
$response["name"] = $Login_model->name;
$response["code"] = $Login_model->code;
$response["description"] = $Login_model->description;
echo json_encode($response);
Login_model.php
require_once 'DB.php';
class Login_model extends DB{
public $code = array();
public $description = array();
public $transporter_car_plates = array();
public $name = array();
public function fetchData(){
//$stmt = $this->db->prepare("SELECT weighting_time, receipt_no FROM wasteman_generate");
$stmt = $this->db->prepare("SELECT wasteman_receive.id, wasteman_receive.warehouse_id, wasteman_receive.transporter_car_plates, warehouses.name, wasteman_receive_parts.receive_id,wasteman_waste_types.code, wasteman_waste_types.description
from wasteman_receive
INNER JOIN warehouses ON wasteman_receive.warehouse_id = warehouses.id
INNER JOIN wasteman_receive_parts ON wasteman_receive.id = wasteman_receive_parts.receive_id
INNER JOIN wasteman_waste_types ON wasteman_receive_parts.waste_type_id = wasteman_waste_types.id
where wasteman_receive.status=1 or wasteman_receive.status=2;");
$stmt->execute();
//$result = $stmt->fetchAll();
//$print_r($result);
foreach($stmt->fetchAll() as $row){
$this->transporter_car_plates[] = array('wasteman_receive.transporter_car_plates' => $row['transporter_car_plates']);
$this->name[] = array('warehouses.name' => $row['name']);
$this->code[] = array('wasteman_waste_types.code' => $row['code']);
$this->description[] = array('wasteman_waste_types.description' => $row['description']);
}
//echo '<br>'.count($this->weighting_time).'<br>';
//echo count($this->weighting_time).'<br>';
}
}
Moj JSon
{"transporter_car_plates":[{"wasteman_receive.transporter_car_plates":"KMY4WL3"},{"wasteman_receive.transporter_car_plates":"KOS65204"}],"name":[{"warehouses.name":"Sortownia 2"},{"warehouses.name":"Sortownia 2"}],"code":[{"wasteman_waste_types.code":"15 01 02"},{"wasteman_waste_types.code":"15 01 02"}],"description":[{"wasteman_waste_types.description":"Opakowania z tworzyw sztucznych"},{"wasteman_waste_types.description":"Opakowania z tworzyw sztucznych"}]}