0

I have contact numbers in local JSON file. And I have to send SMS by retrieving the contact number from JSON. I have no idea how to make it work, please help me.

My JSON file:

{"Person Name": "abc", "Person Phone": "1234567890"}

Here is my method in which I am sending the SMS to fetched conrtact.

protected void sendsms(String Person_Phone,String message)
{


    Toast.makeText(this, "sms sent to"+Person_Phone,Toast.LENGTH_SHORT).show();
try {

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(Person_Phone, null, message, null, null);

    } catch (Exception e) {

    Toast.makeText(getApplicationContext(), "" + getString(R.string.smsfailed),Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }
}
4

4 回答 4

0

尝试这个 :

JSONObject myJsonObject = new JSONObject("{\"Person Name\": \"abc\", \"Person Phone\": \"1234567890\"}");
String number = myJsonObject.optString("Person Phone").toString();
String name = myJsonObject.optString("Person Name").toString();
sendSms(number,someMessage);

在 sendSms 中这样做:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);

在您的 AndroidManifest.xml 文件中包含以下权限

<uses-permission android:name="android.permission.SEND_SMS" />
于 2014-03-18T06:52:00.247 回答
0
JSONObject jsonObj = new JSONObject((String) result);

String personName = jsonObj.getString("Person Name");
String phoneNumber = jsonObj.getString("Person Phone");
于 2014-03-18T05:51:44.677 回答
0

尝试这个..

JSONObject obj = new JSONObject("{\"Person Name\": \"abc\", \"Person Phone\": \"1234567890\"}");
String person_Name = obj.getString("Person Name");
String phone_Number = obj.getString("Person Phone");
于 2014-03-18T05:52:58.850 回答
0

尝试这个

Jsonparser parser=new Jsonparser;
JSONObject obj = parser.parse("{\"Person Name\": \"abc\", \"Person Phone\": \"1234567890\"}");

String phone_no=obj.get("Person Phone").toString;
于 2014-03-18T06:06:30.207 回答