I use Gson classes in my android apps to flatten out ojects and send them over REST to the server and back using HTTP posts and responses. It has always worked well.
But in new app that I am writing I'm trying to do the same thing but using SMS messages instead of HTTP Posts. For instance I have a class ...
class LocReturn
{
public String error; //one char y = error
public String accuracy;
public String hyperlink;
public String stationary; //one char y = stationary
public String speedmph;
public String speedkph;
public String bearing;
}
Before I flatten the class, the hyperlink string gets the following value . . .
http://maps.google.com/maps?z=17&t=h&q=loc:31.7898,-111.0354
However when I examine the json string I see that the hyperlink has been changed to . . .
http://maps.google.com/maps?z\u003d17\u0026t\u003dh\u0026q\u003dloc:31.7898,-111.0354
Also, the SMS send is getting a NullPointer Exception.
The code that send the SMS message is . . .
Gson gson = new Gson();
String jsonstring = gson.toJson(myReturn);
String SMSBody = "###2" + jsonstring;
DebugLog.debugLog("Mole is sending Back: " + " num= " + GlobalStuff.Mobileno + " SMSBody= " + SMSBody, false);
SmsManager
.getDefault()
.sendTextMessage(GlobalStuff.Mobileno, null, SMSBody, null, null);
DebugLog.debugLog("After SMS Send " + SMSBody, true);
finish();
So, (1) I can't see why I'm getting the NullPointer and (2) why is the hyperlink getting changed and does that have anything to do with it.
Thanks,
Gary
EDIT: the new json object creation...
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
did indeed keep the hyperlink from being modified however the nullPointerException continues. It is definitely the sendText line that is causing the exception as when I substitute SMSBody for "hello world" it works fine. The contents of SMSBody is...
###2{"accuracy":"Excellent","bearing":"","error":"n","hyperlink":"http://maps.google.com/maps?z=17&t=h&q=loc:31.7898,-111.0353","speedkph":"","speedmph":"","stationary":"y"}
this is somehow causing the nullPointerException
Answering my own question for future googlers . . .
I forgot that SMS stands for SHORT Message Service. My message was too long and, guess what happens when you send too long a message in SMSManager.sendTextMessage ? You get a nullPointerException (instead of getting a MessageTooLong exception).
Gary
Related
I think this problem is trivial, but I am not advanced in programming. I have problem with displaying Json data to TextView
My Json file is like
{
"person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}
...
}
another data from Json like address, id etc. displaying without problem, when I use
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("person"));
I get string "person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}"
I've tried to do this using
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("firstName") + json.optString("lastName"));
but the result is empty TextView.
Question ==> Is there any method to handle this data and display correctly like substring or something like this?
do this
JSONObject person = json.getJSONObject("person");
((TextView) findViewById(R.id.person)).setText("Person: " + person.getString("firstName") + person.getString("lastName"));
Not entirely sure if this helps but see this.
JSONObject person = jsonObj.getJSONObject("person");
String firstName = person.getString("firstName");
String lastName = person.getString("lastName");
Use strings in an awesome way.
textView.setText(firstName);
I use intents to communicate from a service to the activity. I use intent.putExtra(Tag, message); if I have a error or something.
In the Activity I can get these Extras like following
String message_error_1 = intent.getStringExtra("Message_Error_1");
String message_error_2 = intent.getStringExtra("Message_Error_2");
String message_error_3 = intent.getStringExtra("Message_Error_3");
but just one has data. I can now make an If statement for every entry but I think there is a way to figere out which entry has data. Is there a way?
If there's only going to be one error, I'd consider passing an ERROR_TYPE int as one extra and the ERROR_MESSAGE string as another extra, so you don't have to write so many if statements. For example:
int type = intent.getIntExtra("error_type"); // 1, 2, or 3
String message = intent.getStringExtra("error_message");
To answer your question, you could have the service put in an extra that indicates which error message key to use. For example:
// In Service
intent.putExtra("error_key", "Message_Error_1"); // error 1 has the message!
// In Activity
String key = intent.getStringExtra("error_key");
String actualErrorMessage = intent.getStringExtra(key);
I've got some problem with sending sms from Android app.
In strings.xml I have text1 and text2 with some text.
When I'm trying to send sms with only one string, e.g
sms.sendTextMessage(number, null,message, null,null);
where message i getString(R.string.text1) it works fine. But what I need is send
String text = getString(R.string.text1) + someVariable + getString(R.string.text2); but it doesn't work.
I've tried to make getResources().getString() in both but still nothing, it's not sending;/ What am I doing wrong?
You can use String Builder to append all those string and put it together.
StringBuilder sb = new StringBuilder(some_appropriate_size);
sb.append(getResources().getString(R.string.text1));
sb.append(someVariable);
sb.append(getResources().getString(R.string.text2));
String final_string = sb.toString();
I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}
i am trying to add a two strings to the web service its working fine when i send through browser and it returns "inserted successfully"
but when i run my application its displaying an error as E/Responce(17407): Illegal character in query at index 74: http://purple2.com/beerbro/addgroupon.php?userid=27&data=100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978,1
,
i think there is a problem with "|" this but i am not able to find the solution
here is the code
for(int i=0; i
data=data+friendid[i]+","+status[i]+"|";
data.replaceAll("","%20");
if(i==0){
}
else if(i==10)
{
String res=UrltoValue.getValuefromUrl(DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("res",DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("Responce",res);
data="";
}
if '|' is the problem then use URLEncoder and encode the String
String data = URLEncoder.encode("100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978");
String mainURL = "http://purple2.com/beerbro/addgroupon.php?userid=27&data="+data;
Hope this helps you.