Am I missing something, I am trying to create a loginActivity and I created this code, and I have a log that logs the answer to the volley I created...But The if statement is not getting entered...Can someone see something I'm doing wrong? Sorry if its really dump( and yes I tried to change the response.equals check to be response.equals(" Password Accepted!");, this did not work either)
Another thing to note, yes I know my search in the log is different then the tag, it doesnt login, (which is what i want it to do)
Toast.makeText(getApplication(), response, Toast.LENGTH_LONG).show();
Log.e("Before Conversion",response);
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
L.e("After Conversion",convertedResponse);
if (convertedResponse.equals("Password Accepted!")) {
showProgress(false);
Message msg = new Message();
msg.what = 1;
msg.obj = succcess ? 1 : 0;
handler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
06-29 15:24:04.990 16707-16707/E/Before Conversion: Password Accepted!
06-29 15:24:04.990 16707-16707/E/After Conversion: Password Accepted!
After suggestion to change logging...
06-29 15:34:32.339 16707-16707/ E/Got a response:: Password Accepted!
UPDATE
After creating a JSON array of length 1 with just the response string and a key for it, and passing it to my android studio code, the json array wont parse through the functions used to parse it (JSON Object, JSON Array), but manually parsing the response string works..... Less graceful then I would like it but i guess it works....
This can happen when encoding of response is not UTF-8. Try converting response like this:
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
// do your things ...
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
your response is in JSONObject not JSONArray, so on Response Listnere use:
if(response.getString("response").equals("PASSWORD ACCEPTED!"){
//do what you want to do
}
Related
I'm using RazorPay sdk 1.6.18 in android mobile app
#Override
public void onPaymentError(int code, String response, PaymentData data) {
try {
System.out.println("Payment Error");
String errorMsg = null;
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.has("error")) {
JSONObject errorJson = jsonObject.getJSONObject("error");
if(errorJson.has("description"))
errorMsg = errorJson.getString("description");
}
Toast.makeText(this, errorMsg, Toast.LENGTH_LONG).show();
} catch(Exception e) {
FirebaseCrashlytics.getInstance().recordException(e);
}
}
in onPaymentError() String response is getting empty & it's throwing exception
org.json.JSONException
End of input at character 0 of
We got this issue from crashlytics & it's affecting few users in production.
Does anybody have any clue in which case response will be empty & what's a better way to handle this?
Just Try,
Response strings must be checked for empty, null, or "null" status before being assigned a JSON object. then turn the response string into a JSON object. able to prevent crashes
I am using web services in my application. I use REST services. When I call the web service I get a 200 Response code and correct json response too. But sometimes I get a 200 response code along with an non-json response. So before parsing I need to check whether its a valid json or not. Pls help me.
You can check Json String with the help of following method,
public boolean isJSONValid(String json)
{
try
{
new JSONObject(json);
return true;
}
catch(JSONException ex)
{
return false;
}
}
Use try catch block. Like this:
try {
JSONObject jsonObject = new JSONObject("yourjsonstring");
}
catch(JSONException j) {
System.out.println("Not a JSON");
}
I'm a beginner in android programming.So,I have no idea how to check my login by using PHP. NullPointerException is also appear.I don't know what should I do.Please help for login code.
That's my Jason code.
public void jsonen()
{
ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userID","409"));
String result = response.toString();// store the result returned by PHP script that runs MySQL query
Log.i("response", response);
try{
JSONObject jArray = new JSONObject (result);
for(int i=0;i<jArray.length();i++){
int status = jArray.getInt("requestStatus");
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
The code looks ok (looks horrible actually - well to many if statements and the goto mixed in) and I can't see anything obvious on a php side.
You are using global function db_connect() so make sure that this returns a link to DB.
Other then that you want to read about sql injection:
http://php.net/manual/en/security.database.sql-injection.php
I have to send an encrypted password with DES to a WebServer using a JSONObject encapsulated in a POST request. The problem is that, when I do this:
JSONObject jsonLogin = new JSONObject();
try{
jsonLogin.put("username", usernameS);
jsonLogin.put("password", passwordEncrypted);
}catch (JSONException e){
e.printStackTrace();
}
If I print the content of the JSON object with:
System.out.println("JSON to Server = "+jsonLogin);
the result is:
JSON to Server = {"password":"Qxw\/h16PVdE=\n","username":"XXXXXXXX#gmail.com"}
but the correct password is Qxw/h16PVdE=,so the Server doesn't recognize it.
I found some suggestion that indicate to use: string.replaceAll("\/","/");
But I would like to implement a clean solution.
Please give me any suggestions.
I guess you are doing something wrong while encrypting the data. Any way you can use this piece of code to manipulate the string after encryption.
String encryptedPassword = (String) jsonLogin.get("password");
if (!TextUtils.isEmpty(encryptedPassword) && encryptedPassword.endsWith("\n")) {
encryptedPassword = encryptedPassword.substring(0, encryptedPassword.length() - 1);
jsonLogin.put("password", encryptedPassword);
}
According to this answer
How to remove the \n, \t and spaces between the strings in java?
You can call passwordEncrypted.replaceAll("\\n+", ""); before writing value.
I got a valid Json string(at least every onlinevalidator I tried says so) which contains some chinese characters.
The json string is the following:
{"title":"\u8981\u805e--\u83ef\u723e\u8857\u65e5\u5831","url":"http://cn.wsj.com","desc":"\u300a\u83ef\u723e\u8857\u65e5\u5831\u300b\u4e2d\u6587\u7db2\u7d61\u7248\u6700\u65b0\u8ca1\u7d93\u8981\u805e","imageUrl":null,"lastUpdate":"1327588937","items":[{"title":"\u4e16\u8cbf\u7d44\u7e54\u7e3d\u5e79\u4e8b\ufe55\u4eba\u6c11\u5e63\u5e63\u503c\u88ab\u4f4e\u4f30\ufe50\u4f46\u4f4e\u4f30\u7a0b\u5ea6\u4e0d\u660e","desc":"\u4e16\u754c\u8cbf\u6613\u7d44\u7e54\u7e3d\u5e79\u4e8b\u5e15\u65af\u5361\u723e\uff0e\u62c9\u7c73\u9031\u56db\u8868\u793a\ufe50\u4eba\u6c11\u5e63\u532f\u7387\u88ab\u660e\u986f\u4f4e\u4f30\ufe50\u4f46\u4f4e\u4f30\u7a0b\u5ea6\u9084\u4e0d\u6e05\u695a\u3002","url":"http://cn.wsj.com/big5/20120126/BCHc-20120126WR182711424.asp?source=rss","pubdate":null}]}
Now when i Parse the JsonString in Android via new JsonObject(jsonString) I only got some squares instead of characters.
Why cant android handle this json string?
Any help would be apreciated.
If anyone would like to have the server Ressource, it can be found under:
// edit
url removed
public static String test(){
String testResult= "";
try {
HttpGet get = new HttpGet("http://xxxxx");//edit url removed.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(get);
String result = EntityUtils.toString(response.getEntity());
JSONObject obj = new JSONObject(result);
if(!obj.isNull("title")){
testResult= obj.getString("title");
Log.d("Test","Test1:"+ testResult);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return testResult;//
}
and Then TextView.setText(testResult);
It works for me.
it's Traditional Chinese String:"要聞--華爾街日報"
May be your ROM has no Traditional Chinese font????
When you receive your JSON, you may have to decode the UTF-8 result when you're building your result. Have a look at utf8 decoding. Something similar to this, but with JSON