从Android发送Web请求并从Web应用程序检索响应

时间:2011-12-28 07:14:51

标签: android eclipse webrequest

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

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

现在我想知道何时检测到文本/代码(字符串),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();
 }

2 个答案:

答案 0 :(得分:1)

我无法找到您要发布此字符串的任何网址。但是你可以试试像WebView wv; wv.loadUrl( “http://www.abc.com?action=” + STR);

这将加载url并将字符串传递给Web链接。如果它返回任何响应,如“成功”,则它将显示在屏幕上。您还可以使用HttpConnection进行GET和POST。

答案 1 :(得分:0)

也许这会有所帮助:

Android REST client applications

这并不容易,但这是正确的方法。