通过Android中的Web服务发送SMS

时间:2012-06-30 04:08:35

标签: android android-asynctask

我正在尝试使用网络服务电话发送短信。但是这样做有错误

这是我正在使用

的代码片段
public void sendSMS(String from, String to, String msg) {
        if (stat == -1) {
            sms_url = username + "&password=" + password + "&from=" + from
                    + "&to=" + to + "&message=" + msg + "";
            new SendSMS().execute(sms_url);
        }
    }

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

            for (String url : urls) {
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        result += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            // textView.setText(result);
            Intent intent = new Intent("com.sms.subsahara.CONFIRMACTIVITY");
            startActivity(intent);
        }
    }

    class PostService extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            Login();
            Log.d(LOGTAG, "Success");

            return "success";
        }

        @Override
        protected void onPostExecute(String r) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            String msg = "Login successful";

            if (MenuActivity.status == 2) {
                Log.d(LOGTAG, result);
                Log.d(LOGTAG, error + " " + uid + " " + langId + " " + rank);

            } else if (SendUsing.status == 0) {
                if (result.substring(0, 16).trim().equals(msg)) {
                    Log.d(LOGTAG, " " + "Connection Test" + result);

                    stat = -1;
                    String from = composesms.sender;
                    String to = composesms.phoneNo;
                    String message = composesms.message;
                    sendSMS(from, to, message);
                    Log.d(LOGTAG, result);
                    // new SendSMS().execute(params);

                } else {
                    Toast.makeText(getBaseContext(),
                            "Login UnSuccesful. Check Username or password",
                            Toast.LENGTH_SHORT).show();
                }

            }
        }

在提交按钮上,我有

 public void onClick(View v) {

                if (SendUsing.status == 0) {
                        new PostService().execute("success");
                } 
}

问题是当我这样做时,我得到以下异常

java.lang.IllegalStateException: Target host must not be null, or set in parameters.
   at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
 freed 4919 objects / 298512 bytes in 153ms
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
   **at com.sms.subsahara.WebMessengerActivity$SendSMS.doInBackground(WebMessengerActivity.java:272)
   at com.sms.subsahara.WebMessengerActivity$SendSMS.doInBackground(WebMessengerActivity.java:1)**

请问我该怎么解决这个问题

1 个答案:

答案 0 :(得分:0)

sms_url = username + "&password=" + password + "&from=" + from
                    + "&to=" + to + "&message=" + msg + "";

看起来这行只是制作url的参数而不是完整的URL

 sms_url = "http://your_serviceurl.com?"+sms_url;