【问题标题】:Sending Data from android app to web application将数据从 android 应用程序发送到 Web 应用程序
【发布时间】:2014-07-04 09:04:42
【问题描述】:

我是安卓新手。

要求是我需要从 web 应用程序调用 android 应用程序,然后我需要从 android 应用程序向 web 应用程序发送一个字符串。

这就是我这样做的目的。

1) 创建从网络应用到移动应用的链接(有效)

<a href="intent:#Intent;action=com.appname.demo;end">click to load</a>

2) 移动应用程序有一个文本框和一个按钮,我需要在单击按钮时将文本框的值发送到网络应用程序。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent();
    i.setAction("com.example.helloapp");
    Log.e("IntentTest", i.toUri(Intent.URI_INTENT_SCHEME));

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.editText);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText", mEdit.getText().toString());
                value = mEdit.getText().toString();
        // I need to send value to web application

            }
      });
}

如何将 EditText 值发送到 Web 应用程序。

【问题讨论】:

  • 这里的“网络应用程序”是什么?是你自己的应用吗?
  • @harvey_slash 我开发的 Grails 应用
  • 通过“发送”,您希望启动您的应用程序并能够访问字符串,对吧?

标签: android android-intent web-applications


【解决方案1】:

您想要做的可能是执行 HTTP POST。这在 Java 中很简单,只需使用 HttpURLConnection 类即可。

例如,你可以做这样的事情。

URL url = new URL("www.yoursite.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   

//Send request
DataOutputStream wr = new DataOutputStream (urlConnection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();

try {
 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
 readStream(in); // Your function that reads the result from the server
finally {
 urlConnection.disconnect();
  }
}

http://developer.android.com/reference/java/net/HttpURLConnection.html

【讨论】:

    【解决方案2】:

    您可以使用意图传递字符串

    你可以这样发送:

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
    emailIntent.putExtra("VALUE", valueofthestringyouwanttosend);
        startActivity(intent);
    

    并接收(从您的网络应用程序运行)

    Intent intent = getIntent();
    String value= intent.getStringExtra("VALUE");
    

    【讨论】:

    • 如何在 java 或 grails Web 应用程序中使用 Intent?
    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多