【问题标题】:Open a website and pass data to it using android app打开一个网站并使用 android 应用程序将数据传递给它
【发布时间】:2014-04-15 02:25:21
【问题描述】:

请问如何将我的 android 表单数据的内容传递给网页,并且该数据显示在网页上。

本质上,当我单击我的 android 表单数据的“确定”按钮时,我希望打开网页并显示这些数据。

谢谢。

我正在使用下面的代码 sn-p 打开网页

  public void onClick(View v) {
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://10.0.2.2/test/confirmPayment.php"));      
      new MyAsyncTask().execute(ntwk);
      startActivity(browserIntent);
  }

我正在尝试使用下面的代码传递表单数据。

  public class MyAsyncTask extends AsyncTask<String, Integer, Double>{

@Override
protected Double doInBackground(String... params) {
    // TODO Auto-generated method stub
    postData(params[0]);
    return null;
}

protected void onPostExecute(Double result){
    //pb.setVisibility(View.GONE);
    Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}


private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

public void postData(String valueIWantToSend) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/test/confirmPayment.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("names", valueIWantToSend));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

  }

问题是网页打开但表单数据没有传递。请帮忙。谢谢。

【问题讨论】:

标签: android forms webpage


【解决方案1】:

首先修复您的 getApplicationContext 方法。它返回您的 Toast 使用的 null。其次,如果我没记错的话,您正在打开默认浏览器并将 Http 发布到与您的浏览器连接完全不同的另一个连接。我建议您在 Web 视图中打开您的表单或其他任何内容并覆盖 javascript 方法,以便您可以将数据发布到该 Web 视图。

【讨论】:

  • 更具体地说,搜索更好的 WebViewClients 用法。
猜你喜欢
  • 1970-01-01
  • 2011-02-13
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多