A quick question:
I am getting the error : cannot convert from String to Editable.
Here is the code :
Credential.getInstance().UserName = preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = preferences.getString("UserName", "invalid value");
I've also tried casting like following:
Credential.getInstance().UserName = (Editable) preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = (Editable) preferences.getString("UserName", "invalid value");
This time I am getting error : Cannot cast from String to Editable.
What to do?
For anyone interested in converting a String to Editable
Editable.Factory.getInstance().newEditable("myString");
From the docs, append is
Convenience for append(String.valueOf(text)).
Surely:
Credential.getInstance().UserName.append(preferences.getString("UserName", "invalid value"));
The API with the answers:
http://developer.android.com/reference/android/text/Editable.html
Related
I used this:
String message += getResources().getString(R.string.string1) + "some more word...";
and I wanted to send this string via sms, but it is not working. It works fine without the string resource. Am I missing something?
#forpas answer is absolutely correct, but you can also concat string resource this way.
<string name="name">Name %s</string>
String nameText = getString(R.string.name,"khemraj");
When you use += operator with a String the result is a concatenation of the previous value of the String with some new String.
When you define a String variable like this:
String s;
the variable s is not initialized, so this:
s+="something";
is not allowed.
So instead of
String message += getResources().getString(R.string.string1) + "some more word...";
do
String message = getResources().getString(R.string.string1) + "some more word...";
Basically I want to convert a JSONObject json into an integer (127181078). When I use this code:
int intOfReceivedID = Integer.parseInt(json);
I get this error message:
java.lang.NumberFormatException: Invalid int: 127181078"
When I use this code:
char[] charArray = json.toCharArray();
String stringCharArray = charArray.toString();
testTextView.setText(stringCharArray);
testTextView gives me: [C#2378e625
However, when I use this code:
preprocessedjson = String.valueOf(json);
testTextView.setText(preprocessedjson);
The TextView gives 127181078, but I get the some error when I parse the text to an integer.
Can anybody tell me what going on here?
And could you help me convert my JSONObject into an integer?
This is the php code:
$myfile = fopen($filename, 'r') or die("Unable to open file!");
echo fread($myfile,filesize($filename));
fclose($myfile);
This is the the makeHttpRequest:
JSONObject json = jparser.makeHttpRequest("http://myurl.nl/readspecifictextfile.php","POST",data);
This question is confusing but from your error message it looks like this is a Java question that has nothing to do with JSON since your json String in this case looks like it does not in fact contain json(from the exception message).
It looks like the issue is your json value is a number plus additional spaces which Integer.parseInt does not handle.
Try
Integer.parseInt(json.trim())
int intOfReceivedID = Integer.parseInt(json);
I get this error
message:
java.lang.NumberFormatException: Invalid int: 127181078"
This happens because there is a new-line character at the end of the ID which can not be parsed into an Integer.
When I use this code:
char[] charArray = json.toCharArray();
String stringCharArray = charArray.toString();
testTextView.setText(stringCharArray);
testTextView gives me: [C#2378e625
By default, calling toString() on an object prints the memory location of the object (in this case [C#2378e625). That's what's being displayed in the TextView.
However, when I use this code:
preprocessedjson = String.valueOf(json);
testTextView.setText(preprocessedjson);
The TextView gives 127181078, but I get the some error when I parse
the text to an integer.
You'll get an error when parsing the text to an integer because it still has an invalid new-line character at the end.
If you are receiving a JSONObject from the server which only contains a long, then you can use the getLong() or optLong() methods to retrieve the integer value. The JSON parser automatically handles all the parsing and you don't need to do any additional work.
JSONObject json = jparser.makeHttpRequest("http://myurl.nl/readspecifictextfile.php","POST",data);
final long receivedId = json.optLong();
I'm getting the password-edit-text value but I don't get what I want.
my code in getting the value:
String username = "", password = "";
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
but when I put the result to Toast I don't get it right. What I get is this
error.
any help will be much appreciated. Thanks!
Your code would return you the id of your EditText yet what you want is the Text.
Try this
Replace this by :
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
this
username = ((EditText)findViewById(R.id.username_pet)).getText();
password = ((EditText)findViewById(R.id.password_pet)).getText();
Hope this helps
username = ((EditText)findViewById(R.id.username_pet)).getText().toString();
password = ((EditText)findViewById(R.id.password_pet)).getText().toString();
This is happening because you are attempting to convert to string before getting the text of the input field. The app is doing exactly what you are telling it to do: Getting the EditText View and converting the View to a string, just not the text within the view. If that makes sense.
Try
EditText userName = (EditText) findViewById(R.id.username_pet);
String userNameFromEditText = (String) username.getText().toString();
Then toast the value of userNameFromEditText.
String email = email.getText().toString();
Please help getting error The method getText() is undefined for the type String
Change - String email = email.getText().toString() to
String emailStr = email.getText().toString()
It seems that you have shadowed an attribute email by calling a variable with the same name.
Hence email does not reference what you meant anymore (a textfield I suppose) but a String.
You should change the name of your variable.
Assume the id of your EditText is email. To get the Text of the EditText you can use
String email = ((EditText)findViewById(R.id.email)).getText().toString();
My JSON stream can be different each time. For example sometime it can include a "Song" field and sometime not.
I am getting this fields value asText ? How to tell Jackson to get this value as an Empty String if it is not defined ?
Example
"Content": "MusicContent",
"Song": "Track_1",
if try node.get("Song").asText() it will give "Track_1"
"Content": "MusicContent",
Now , if i try to get node.get("Song") it gives null pointer exception. I want to get an empty string when calling asText().
How can i do that ?
Thanks
You could check for null before calling the asText() on the node. i would probably do it like this :
if (node.get("Song") != null){
myString = node.get("Song").asText();
} else {
myString = "";
}
Or in a fancy way like this :
myString = ((node.get("Song")!=null) ? node.get("Song").asText() : "");