【问题标题】:Sending a web request from Android and to retrieve back response from web application从 Android 发送 Web 请求并从 Web 应用程序检索响应
【发布时间】:2011-12-28 07:14:50
【问题描述】:

我想在发送网络请求方面寻求帮助。

在第 1 部分中,我必须检测短信中的某些文本/代码,这已完成。

现在我想知道何时检测到文本/代码(字符串),android 是如何将 web 请求发送到 java web 应用程序的?

我需要使用 post 和 get 方法吗?

使用:Eclipse Indigo 2.7,API 2.3.3

建议和任何帮助将不胜感激 =)

以下是我现有的代码:

// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";

 if (bundle != null) // if contains message
 {

 // ---retrieve the SMS message received---
 Object[] pdus = (Object[]) bundle.get("pdus");
 msgs = new SmsMessage[pdus.length];
 for (int i = 0; i < msgs.length; i++)

   {
    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
    str += "SMS from " + msgs[i].getOriginatingAddress();
    str += " :";
          str += msgs[i].getMessageBody().toString();
    str += "\n";

   Log.i(TAG, "Message: "+msgs[i].getMessageBody().toString()); //The message content


    // Checking for Pattern
    // Direct use of Pattern:
    Pattern p = Pattern.compile("123");
    Matcher m = p.matcher(msgs[i].getMessageBody().toString());


    while (m.find()) // Find each match in turn; String can't do this.
          { 
             String result = m.group(); // Access a submatch group;
             Log.i(TAG, "Password Match: " + result);   //Showing the result
             str +="found";
          }
           } 

 // ---display the new SMS message---
 Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
 }

【问题讨论】:

    标签: android eclipse webrequest


    【解决方案1】:

    我找不到您要发布此字符串的任何网址。但是,您可以尝试 WebView wv 之类的方法; wv.loadUrl("http://www.abc.com?action="+str);

    这将加载 url 并将字符串传递给 web 链接。如果它返回任何诸如“成功”之类的响应,那么它将显示在屏幕上。您还可以将 HttpConnection 用于 GET 和 POST。

    【讨论】:

    • 我可能想使用 GET 和 POST 方法,是否有任何示例可以帮助我了解更多,以便我知道如何在我的代码中实现该方法?
    • HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("example.com"); List nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("account", "login")); nameValuePairs.add(new BasicNameValuePair( “电子邮件”、“email@email.com)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse 响应 = httpclient.execute(httppost);试试上面的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2011-12-21
    • 2021-03-13
    • 2015-01-06
    相关资源
    最近更新 更多