Я продолжаю получать исключение JSON. Данные не загружаются.
PHP-код:
mysql_connect("somedbuser.byethost13.com","user", "pass") or die(mysql_error()); mysql_select_db("some_database"); $id = $_GET["id"]; $sql=mysql_query("select * from brewery_data where id between ($id+1) and ($id+2)"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; header('Content-Type:Application/json'); echo(json_encode($output)); mysql_close(); ?>
public class BrowseBrewery extends MainActivity { private AutoCompleteTextView actvBreweryName; private AutoCompleteTextView actvBreweryState; private RecyclerView recyclerView; private LinearLayoutManager linearLayoutManager; private CustomAdapter adapter; private List<BreweryData> brewery_data_list; String[] breweryNames; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browse_brewery); actvBreweryName = (AutoCompleteTextView) findViewById(R.id.actv_brewery_name); actvBreweryState = (AutoCompleteTextView) findViewById(R.id.actv_state); cbAdditives = (CheckBox) findViewById(R.id.cb_additives); recyclerView = (RecyclerView) findViewById(R.id.brewery_recycler_view); brewery_data_list = new ArrayList<>(); load_data_from_server(0); linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); adapter = new CustomAdapter(this, brewery_data_list); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == brewery_data_list.size()-1) { load_data_from_server(brewery_data_list.get(brewery_data_list.size()-1).getId()); } } }); for (int i = 0; i < brewery_data_list.size(); i++) { breweryNames[i] = brewery_data_list.get(i).getName(); } ArrayAdapter<String> stateAdapter = new ArrayAdapter<> (this, android.R.layout.select_dialog_item, states); actvBreweryState.setThreshold(1); actvBreweryState.setAdapter(stateAdapter); } private void load_data_from_server(final int id) { AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() { @Override protected Void doInBackground(Integer... integers) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://thebeerguru.byethost13.com/conn_all.php?id="+id).build(); try { Response response = client.newCall(request).execute(); JSONArray array = new JSONArray(response.body().string()); for (int i=0; i<array.length(); i++) { JSONObject object = array.getJSONObject(i); BreweryData data = new BreweryData( object.getInt("id"), object.getString("name"), object.getString("image_link"), object.getString("city"), object.getString("state"), object.getString("phone"), object.getString("website"), object.getString("year_established"), object.getInt("rating"), object.getString("featured")); brewery_data_list.add(data); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { System.out.println("End of Content"); } return null; } @Override protected void onPostExecute(Void aVoid) { adapter.notifyDataSetChanged(); } }; task.execute(id); } }
-public class BrowseBrewery extends MainActivity { private AutoCompleteTextView actvBreweryName; private AutoCompleteTextView actvBreweryState; private RecyclerView recyclerView; private LinearLayoutManager linearLayoutManager; private CustomAdapter adapter; private List<BreweryData> brewery_data_list; String[] breweryNames; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browse_brewery); actvBreweryName = (AutoCompleteTextView) findViewById(R.id.actv_brewery_name); actvBreweryState = (AutoCompleteTextView) findViewById(R.id.actv_state); cbAdditives = (CheckBox) findViewById(R.id.cb_additives); recyclerView = (RecyclerView) findViewById(R.id.brewery_recycler_view); brewery_data_list = new ArrayList<>(); load_data_from_server(0); linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); adapter = new CustomAdapter(this, brewery_data_list); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == brewery_data_list.size()-1) { load_data_from_server(brewery_data_list.get(brewery_data_list.size()-1).getId()); } } }); for (int i = 0; i < brewery_data_list.size(); i++) { breweryNames[i] = brewery_data_list.get(i).getName(); } ArrayAdapter<String> stateAdapter = new ArrayAdapter<> (this, android.R.layout.select_dialog_item, states); actvBreweryState.setThreshold(1); actvBreweryState.setAdapter(stateAdapter); } private void load_data_from_server(final int id) { AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() { @Override protected Void doInBackground(Integer... integers) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://thebeerguru.byethost13.com/conn_all.php?id="+id).build(); try { Response response = client.newCall(request).execute(); JSONArray array = new JSONArray(response.body().string()); for (int i=0; i<array.length(); i++) { JSONObject object = array.getJSONObject(i); BreweryData data = new BreweryData( object.getInt("id"), object.getString("name"), object.getString("image_link"), object.getString("city"), object.getString("state"), object.getString("phone"), object.getString("website"), object.getString("year_established"), object.getInt("rating"), object.getString("featured")); brewery_data_list.add(data); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { System.out.println("End of Content"); } return null; } @Override protected void onPostExecute(Void aVoid) { adapter.notifyDataSetChanged(); } }; task.execute(id); } }
JSON DATA:
[{ "id": "1", "name": "57 Brew Pub & Bistro", "image_link": "https:\/\/static.wixstatic.com\/media\/d39f67_ffb7b66c09694eea85479d7a478b64a7~mv2.png\/v1\/fill\/w_278,h_209,al_c,usm_0.66_1.00_0.01\/d39f67_ffb7b66c09694eea85479d7a478b64a7~mv2.png", "city": "Greenville", "state": "MI", "phone": "(616) 712-6226", "website": "57brewpub.com", "year_established": "NA", "rating": "0", "featured": "true" }]