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();
    }
}
Yogesh D
  • 1,663
  • 2
  • 23
  • 38

4 Answers4

0
JSONObject jsonObj = new JSONObject((String) result);

String personName = jsonObj.getString("Person Name");
String phoneNumber = jsonObj.getString("Person Phone");
Amresh
  • 2,098
  • 16
  • 20
  • SMS is not sent...I have written a separate method for sending SMS and giving a toast to see if SMS is sent but its not sent..i have posted my SMS method – user2392465 Mar 18 '14 at 06:20
  • write the complete code... from fetching JSON data to sending sms. – Amresh Mar 18 '14 at 06:38
0

Try this..

JSONObject obj = new JSONObject("{\"Person Name\": \"abc\", \"Person Phone\": \"1234567890\"}");
String person_Name = obj.getString("Person Name");
String phone_Number = obj.getString("Person Phone");
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • I have a similar use case here: https://stackoverflow.com/questions/62239517/android-deliver-plain-text-data-to-another-app-user-with-clickable-image-via-sm Would appreciate any ideas, thoughts that you could offer – AJW Jun 10 '20 at 01:22
0

Try this

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

String phone_no=obj.get("Person Phone").toString;
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
0

Try this :

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);

In sendSms do this:

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

Include the following permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.SEND_SMS" />
Yogesh D
  • 1,663
  • 2
  • 23
  • 38