2017-01-17 8 views
0

私はいくつかの問題に直面しています。私はSMS本体とアドレスを取得していて、WebサービスでそのSMS本体/アドレスを送信しますが、値は重複しています。私は次の問題に直面しています:Android SMSサービスのSMSアドレスと本文を1つずつ送信する

1回ごとに受信トレイに3つのメッセージがあれば、ループは3回実行され、1つのメッセージが繰り返し送信されますウェブサービス。

2 - 私は、私はSMSのボディとアドレスを取得するときは、asynタスクが呼び出され、非同期タスクが最後まで実行された後、最初の反復後に再びasyncallが2回目の反復などのために呼び出されることを望みます。

私の質問は非常に混乱していますが、plzヘルプは、どんな助けも高く評価されます。私のコードは以下のとおりです。

public void onClick(View v) { 

    ContentResolver contentResolver = getContentResolver(); 
    Cursor cursor = contentResolver.query(Uri.parse("content://sms/inbox"), 
      null, null, null, null); 

    int count = 0; 

    int indexBody = cursor.getColumnIndex(SmsReceiver.BODY); 
    int indexAddr = cursor.getColumnIndex(SmsReceiver.ADDRESS); 

    if (indexBody < 0 || !cursor.moveToFirst()) 
     return; 

    smsList.clear(); 

    do { 
     strmobilenum = cursor.getString(indexAddr); 
     // str = "0" + str.substring(3); 
     strmessagebody = cursor.getString(indexBody); 
     String mb = strmobilenum; 
     String mbody = strmessagebody; 
     //SendSMSToServer(); 
     new AsyncCallWS().execute(); 
    } while (cursor.moveToNext()); 
} 

private class AsyncCallWS extends AsyncTask<String, Void, Void> { 
    @Override 
    protected Void doInBackground(String... params) { 

     try { 

      request = new SoapObject(NAMESPACE, METHOD_NAME); 


      PropertyInfo unameProp = new PropertyInfo(); 
      unameProp.setName("SMSSenderMobileNo");// Define the variable name in 
       String mobnum = strmobilenum;         // the web // service method 
      unameProp.setValue(strmobilenum);// set value for userName variable 
      unameProp.setType(String.class);// Define the type of the variable 
      request.addProperty(unameProp);// Pass properties to the variable 

      PropertyInfo textProp = new PropertyInfo(); 
      textProp.setName("SMSText");// Define the variable name in the web // 
         String msgbody = strmessagebody;    // service method 
      textProp.setValue(strmessagebody);// set value for userName variable 
      textProp.setType(String.class);// Define the type of the variable 
      request.addProperty(textProp);// Pass properties to the variable 

      // SSLConection.allowAllSSL(); 
      envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.setOutputSoapObject(request); 
      envelope.dotNet = true; 

      // androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport = new HttpTransportSE(URL, 60000); 



      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 

      String jsonResponse = ""; 
      jsonResponse = response.toString(); 

      // A JSONTokener is needed in order to use JSONObject correctly 
      JSONTokener jsonTokener = new JSONTokener(jsonResponse); 
      // Pass a JSONTokener to the JSONObject constructor 
      JSONObject jsonObj = new JSONObject(jsonTokener); 
      JSONArray data = jsonObj.getJSONArray("FeedSMS"); 

      if (data != null) { 

       // looping through All nodes 
       for (int i = 0; i < data.length(); i++) { 
        JSONObject c = data.getJSONObject(i); 
        RetVal = c.getString("RetVal"); 
        RetDes = c.getString("RetDes"); 

       } 

       /* 
       * Toast.makeText(getBaseContext(), "Success", 
       * Toast.LENGTH_LONG).show(); 
       */ 

      } else { 


     } catch (Exception e) { 
      // alertDialog(e.toString()); 
      /* 
      * Toast.makeText(getApplicationContext(), e.getMessage(), 
      * Toast.LENGTH_LONG).show(); 
      */ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Log.i(TAG, "onPostExecute"); 
     // tv.setText(fahren + "° F"); 

     pDialog.dismiss(); 

     if (RetVal != null) { 

      // Toast.makeText(getApplicationContext(), message, 
      // Toast.LENGTH_SHORT).show(); 

      if (RetVal.equalsIgnoreCase("T")) { 

       alertDialog(RetDes); 

      } else { 

       alertDialog(RetDes); 

      } 
     } else { 
      //alertDialog("server error"); 
     } 
    } 

    @Override 
    protected void onPreExecute() { 

     pDialog.setMessage("Sending SMS..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     // Log.i(TAG, "onProgressUpdate"); 
    } 

} 

答えて

0

あなたは、カーソル位置が最初に0をあるとして、常に最初のSMSの値が含まれますindexBodyとindexAddr上記のwhileループ

int indexBody = cursor.getColumnIndex(SmsReceiver.BODY); 
int indexAddr = cursor.getColumnIndex(SmsReceiver.ADDRESS); 

if (indexBody < 0 || !cursor.moveToFirst()) 
    return; 

外のインデックスを取得しています。 正常ので、その後、第二は、Webサービスに送信される第一のSMSを送信した後、正常にWebサービスに送る私はちょうどその第一SMSボディにしたい

do 
{ 
    int indexBody = cursor.getColumnIndex(SmsReceiver.BODY); 
    int indexAddr = cursor.getColumnIndex(SmsReceiver.ADDRESS); 
    if (indexBody < 0 || !cursor.moveToFirst()) 
     return; 
    strmobilenum = cursor.getString(indexAddr); 
    // str = "0" + str.substring(3); 
    strmessagebody = cursor.getString(indexBody); 
    String mb = strmobilenum; 
    String mbody = strmessagebody; 
    //SendSMSToServer(); 
    new AsyncCallWS().execute(); 
} 
+0

次のようにループを行うに上記のコードを書きますすべてのSMSが読み込まれてWebサービスに送信されるまでオンにします。 – newOnAndroid

+0

これを実現するには簡単な方法が必要ですが、私ができることが分かっている限り、すべての数字と文字をカーソルから取得し、arraylistに保存してください。次に、arraylistの値を追加するasyncTaskを開始すると、statusCode = 200になると、arraylistの次の位置でasyncTaskをもう一度呼び出す –

+0

答えが役立ちましたか?はいの場合は答えを受け入れてください。そうでない場合は、どのように問題を解決しましたか教えてください。 –

関連する問題