0

I keep receiving a JSON exception. Data will not load.

PHP CODE:

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);
    }
}

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": "N.A.",
    "rating": "0",
    "featured": "true"
}]
Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47

1 Answers1

0

The problem is that the web host that I am utilizing (Byet Host) implements a simple security antibots module named testcookie-nginx-module for L7 DDoS attack mitigation.

https://kyprizel.github.io/testcookie-nginx-module/

The testcookie-nginx-module makes a 2-step validation:

1) The first time that a HTTP request is done, the module returns a javascript instead of the JSON we are expecting. This script is executed on the client (tipically a web browser) and generates a validation cookie containing an AES key.

2) The script adds the validation cookie to the document and redirects it to the url we actually want to access. The testcookie-nginx-module validates the cookie AES key and lets the request hit the url that will respond with the JSON data we want to access. On the following HTTP requests the client will have stored the cookie and will add it to the request skipping the step 1.

We just need to add a cookie to the HTTP request to pass the testcookie-nginx-module.

These links should help optimize the code and aid future developers with the same issue:

ByetHost server passing html values "Checking your browser" with JSON String

and

Add cookie to client request OkHttp

:-)

Simply calling .addHeader worked for me.

        @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)
                    .addHeader("Cookie", "__test=THE_CONTENT_OF_MY_COOKIE; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/")
                    .build();

            try {
                Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().string());

Thanks to everyone for their counsel and support!

Community
  • 1
  • 1