【问题标题】:Unable to get data at first click in android webview with server communication在与服务器通信的 android webview 中第一次单击时无法获取数据
【发布时间】:2015-06-08 05:59:17
【问题描述】:

我从 JavaScript 调用这个方法。
这是第一次,它是空的。但是从第二次开始,数据来了。
请帮帮我。

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

这里我们通过 Android 中的 AsyncTask 获取数据:

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

    }
}

此方法用于从服务器获取数据

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

我正在从服务器获取 json 字符串格式的数据。
它显示具有空值的属性,例如 {pos["posid":null, "storeName":null]} 等。

【问题讨论】:

标签: javascript android html httpclient


【解决方案1】:

在下面的函数中

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

你执行你的HttpAsyncTaskClickedData(),并立即返回bdb.getmPosIdData()(这始终是第一次出现的问题)

AsyncTask 的结果仅在 onPostExecute 中可用,您在其中调用 bdb.setmPosIdData(result);

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

HttpAsyncTaskClickedData 在后台运行,可能需要一些时间。
这就是为什么您的第一次调用总是无法获取数据的原因,因为您在执行 AsyncTask 后立即返回了bdb.getmPosIdData()

【讨论】:

  • 我正在取消 onPostExecute 中的对话,尽管结果与上面相同。
  • 你的意思是pDialog.dismiss();onPostExecute里面?
  • 是的。在将结果设置为 setmPosIdData() 之前关闭对话。
  • @harika 它与对话框无关。你看过我的回答吗?您在执行 AsyncTask 后立即返回 bdb.getmPosIdData(),这将返回 null,直到您设法获取数据并在 onPostExecute 中使用 bdb.setmPosIdData() 设置它。
  • @harika 所以,关键是只有在您使用bdb.setmPosIdData() 实际存储了一些内容之后,才能使用bdb.getmPosIdData() 获取您的数据。如果有帮助,请采纳答案,谢谢。
【解决方案2】:
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. 

【讨论】:

    【解决方案3】:

    此功能用于在javascript中使用post url获取数据

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多