Skip to content Skip to sidebar Skip to footer

Android&php - How To Display Jsonarray From Mysql Using Php

The code that I use to displaying value of table into JSONArray below send_data.php

Solution 1:

Problem

HttpURLConnection has no JavaScript support, but a needed cookie is generated using JavaScript.

Your call

StringreqUrl="http://zxccvvv.cuccfree.com/send_data.php";
URLurl=newURL(reqUrl);
HttpURLConnectionconn= (HttpURLConnection) url.openConnection();

fails, because the cookie __test is missing.

Fix

From a first glance at the JavaScript source the cookie seems to be constant for a given url, so it might be enough to set for a constant cookie:

Stringcookie="__test=2bf7c15501c0ec57f8e41cb84871a69c";

URLurl=newURL(reqUrl);
HttpURLConnectionconn= (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(7000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Cookie", cookie);

Alternative: Using a WebView we can grab the cookie, so this is the preferable approach, since it will not break, if the cookie changes and it is not much of a time delay:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getCookie();

    if(cookie!=null) {
        newGetContacts().execute();
    }
}

privatevoidgetCookie(){
    CookieManager.setAcceptFileSchemeCookies(true);
    WebView wv = newWebView(getApplicationContext());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(url);
    cookie = CookieManager.getInstance().getCookie("zxccvvv.cuccfree.com");
}

and set it as in above example:

conn.setRequestProperty("Cookie", cookie);

Output in logcat

Response from url: [{"0":"1","id":"1","1":"pertanyaan ke 1","ask":"pertanyaan ke 1"},{"0":"2","id":"2","1":"pertanyaan ke 2","ask":"pertanyaan ke 2"},{"0":"3","id":"3","1":"pertanyaan ke 3","ask":"pertanyaan ke 3"},{"0":"4","id":"4","1":"pertanyaan ke 4","ask":"pertanyaan ke 4"},{"0":"5","id":"5","1":"pertanyaan ke 5","ask":"pertanyaan ke 5"}]

Post a Comment for "Android&php - How To Display Jsonarray From Mysql Using Php"