Unable to get data at first click in android webview with server communication

188 views Asked by At

I am calling this method from JavaScript.
For the first time, it is giving null. But from the second time onwards, the data is coming.
Please help me.

@SuppressLint("JavascriptInterface")
public String loadClickedData() {

    pDialog = ProgressDialog.show(this, "dialog title",
            "dialog message", true);
    new HttpAsyncTaskClickedData()
            .execute("http://192.168.0.9/btrepo/btrepo/android_api.php");
    return bdb.getmPosIdData();
}

Here we are getting data through AsyncTask in Android:

private class HttpAsyncTaskClickedData extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("tag", "get_pos_details"));
        nameValuePairs.add(new BasicNameValuePair("pos_id", posId));
        return GET(urls[0], nameValuePairs);
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        // Toast.makeText(getApplicationContext(), result+" in post ",
        // Toast.LENGTH_LONG).show();
        pDialog.dismiss();
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG)
                .show();
        bdb.setmPosIdData(result);

    }
}

This method is for getting data from the server:

public static String GET(String url, List<NameValuePair> pair) {
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(pair));

        HttpResponse httpResponse = httpClient.execute(httpPost);

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        is = inputStream;
        // convert inputstream to string
        if (inputStream != null) {
            // jObj = convertIsToJson(inputStream);
            result = convertIsToJson(inputStream) + " ";
            // jSonStr = result;
        } else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

I am getting data from the server in json string format.
It is displaying attributes with null value like {pos["posid":null, "storeName":null]} etc..

3

There are 3 answers

5
almightyGOSU On BEST ANSWER

In the following function

public String loadClickedData() {

    pDialog = ProgressDialog.show(this, "dialog title",
            "dialog message", true);
    new HttpAsyncTaskClickedData()
            .execute("http://192.168.0.9/btrepo/btrepo/android_api.php");
    return bdb.getmPosIdData();
}

You execute your HttpAsyncTaskClickedData(), and return bdb.getmPosIdData() immediately. (Which will always be an issue for the 1st time)

The result of the AsyncTask is only available in onPostExecute, where you call bdb.setmPosIdData(result);.

@Override
protected void onPostExecute(String result) {
    // ...
    bdb.setmPosIdData(result); // result is only stored in bdb here!
}

The HttpAsyncTaskClickedData runs in the background, and possibly takes some time.
That is why your first call is always unable to get data, as you returned bdb.getmPosIdData() immediately after executing your AsyncTask.

0
harika On

This function is for get data by using post url in javascript

 function jsonPost(){
    var http = new XMLHttpRequest();
    var url = "http://192.168.0.9/btrepo/btrepo/android_api.php";
var params = "tag=fav_stores&mobile=984989874";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
  alert("hello "+http.send(params));




    }
0
harika On
private class HttpAsyncTaskClickedData extends
            AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs
                    .add(new BasicNameValuePair("tag", "get_pos_details"));
            nameValuePairs.add(new BasicNameValuePair("pos_id", posId));
            return GET(urls[0], nameValuePairs);
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            // Toast.makeText(getApplicationContext(), result+" in post ",
            // Toast.LENGTH_LONG).show();
            data = result;
            pDialog.dismiss();
            bdb.setmPosIdData(result);

        }

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            pDialog = ProgressDialog.show(MainActivity.this, "dialog title",
                    "dialog message", true);
        }
         @SuppressLint("JavascriptInterface")
         public String loadData() {
         Toast.makeText(getApplicationContext(), data+ " hi ",
         Toast.LENGTH_LONG).show();
         return data;
        }
    }

and calling loadData() method from javascript.