I think that JSONException only works when it my request fails but when the request is valid (valid username, password) it should redirect me to another activity but instead a JSONException is showing up.
It shows the JSON string received from the server rather than redirecting me to another activity.
This is my onResponse function
#Override
public void onResponse(String response){
try {
JSONObject volleyResponse = new JSONObject(response);
boolean success = volleyResponse.getBoolean("success");
String message = volleyResponse.getString("message");
String UUID = volleyResponse.getString("unique_user_id");
String LLOGIN = volleyResponse.getString("last_login");
String RDATE = volleyResponse.getString("registration_date");
String MONEY = volleyResponse.getString("money");
if(success){
Intent intent = new Intent(Authentication.this, Mainpage.class);
intent.putExtra(KEY_USERNAME, strUsername);
intent.putExtra(KEY_UUID, UUID);
intent.putExtra(KEY_LLOGIN, LLOGIN);
intent.putExtra(KEY_RDATE, RDATE);
intent.putExtra(KEY_MONEY, MONEY);
startActivity(intent);
}
} catch(JSONException e) {
response = response.replace("\"", "");
response = response.replace("status:false,message:", "");
response = response.replace("{", "");
response = response.replace("}", "");
messageText.setText(response);
}
}
JSON Response when it is success:
{"unique_user_id":"4e99a28a-0cb2-30a9-ac51-ccd4629bcef1","last_name":"therealaxis","password":"$2a$10$9qRjW\/vJreCQg3u5dO6eW.8PhZBTpGaPNK5qRIYP.XTx2PVY1yrOi","last_login":"1 week ago","registration_date":"1 week ago","money":"100.00","success":true}
Your JSON Response has no message string, so a JSONException is thrown. If you just want to access the message attribute in case it is present, use JSONObject.has before accessing it.
Related
So I have this POST request made to the server and based on an argument the server will return error message within the errorBody() of Retrofit. I am trying to handle that Plain Text error returned by the server and then display it to the user within my Android application which uses Java. Below is my current attempt but this is giving me this error in Logcat:
#Url cannot be used with #POST URL (parameter #1)
Here is 400 response from the server:
Interface:
public interface ChangePickLocationClient
{
#GET
Call<ResponseBody> checkItem(#Url String url, #Header("Authorization") String authToken);
#GET
Call<String> getStringError(#Url String url, #Header("Authorization") String authToken);
#POST("Pick/ChangePickLocationAcceptChange")
Call<String> changePickLocationPOST(#Url String url, #Header("Authorization") String authToken, #Body
ChangePickLocationPostModel changePickLocationPostModel);
}
Implementation:
private static final String BASE_URL = "http://00.00.00.1234/api/";
Gson mGson = new Gson();
Retrofit retrofit = new Retrofit.Builder().client(new OkHttpClient())
.baseUrl(BASE_URL).addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(mGson))
.build();
ChangePickLocationClient ChangePickLocationClient =
retrofitPOST.create(ChangePickLocationClient.class);
String itemNumber = itemNumberValue.getText().toString();
newPickLocationValue.setText(newPickLocationValue.getText().toString().toUpperCase());
String newPickLocation = newPickLocationValue.getText().toString();
String token = globalClass.getActiveToken();
final ChangePickLocationClient mChangePickLocationInterface =
retrofit.create(ChangePickLocationClient.class);
Call<String> mCallErrorPOST = mChangePickLocationInterface.changePickLocationPOST
(postUrl, "Bearer " + globalClass.getActiveToken(),
changePickLocationPostModel);
call.enqueue(new Callback<ChangePickLocationPostModel>()
{
#Override
public void onResponse(Call<ChangePickLocationPostModel> call,
Response<ChangePickLocationPostModel> response)
{
String mPlainTextResponse = null;
try {
if(response.errorBody() != null)
{
mPlainTextResponse = response.errorBody().string();
}
} catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(ChangePickLocation.this, mPlainTextResponse
,Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(Call<ChangePickLocationPostModel> call, Throwable t)
{
Toast.makeText(ChangePickLocation.this, "Unknown server error!"
,Toast.LENGTH_SHORT).show();
}
});
When the response is 400, the second call being made needs to be a clone() call. This is because the Call cannot be used more than once as stated in the documentation.
use this:
call.clone().enqueue(new Callback<ChangePickLocationPostModel>()
instead of
call.enqueue(new Callback<ChangePickLocationPostModel>()
When sending a request in Postman, I get this output:
{
"valid": false,
"reason": "taken",
"msg": "Username has already been taken",
"desc": "That username has been taken. Please choose another."
}
However when doing it using okhttp, I get encoding problems and can't convert the resulting json string to a Java object using gson.
I have this code:
public static void main(String[] args) throws Exception {
TwitterChecker checker = new TwitterChecker();
TwitterJson twitterJson = checker.checkUsername("dogster");
System.out.println(twitterJson.getValid()); //NPE
System.out.println(twitterJson.getReason());
System.out.println("Done");
}
public TwitterJson checkUsername(String username) throws Exception {
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://twitter.com/users/username_available").newBuilder();
urlBuilder.addQueryParameter("username", username);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json; charset=utf-8")
.build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
Response response = call.execute();
System.out.println(response.body().string());
Gson gson = new Gson();
return gson.fromJson(
response.body().string(), new TypeToken<TwitterJson>() {
}.getType());
}
Which prints this:
{"valid":false,"reason":"taken","msg":"\u0414\u0430\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u043e","desc":"\u0414\u0430\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u043e. \u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0434\u0440\u0443\u0433\u043e\u0435."}
and then throws a NullPointerException when trying to access a twitterJson. Debugger shows that object as being null.
TwitterJson:
#Generated("net.hexar.json2pojo")
#SuppressWarnings("unused")
public class TwitterJson {
#Expose
private String desc;
#Expose
private String msg;
#Expose
private String reason;
#Expose
private Boolean valid;
public String getDesc() {
return desc;
}
public String getMsg() {
return msg;
}
public String getReason() {
return reason;
}
public Boolean getValid() {
return valid;
}
...
How can I fix the encoding issues with okhttp?
It is because the response object can be consumed only once. OKHTTP says that in their documentation. After the execute is invoked, you are calling the response object twice. Store the result of response.body().string() to a variable and then do the convert into GSON.
If I were to use a hello world example...
private void testOkHttpClient() {
OkHttpClient httpClient = new OkHttpClient();
try {
Request request = new Request.Builder()
.url("https://www.google.com")
.build();
Call call = httpClient.newCall(request);
Response response = call.execute();
System.out.println("First time " + response.body().string()); // I get the response
System.out.println("Second time " + response.body().string()); // This will be empty
} catch (IOException e) {
e.printStackTrace();
}
}
The reason it is empty the second time is because the response object can be consumed only once. So you either
Return the response as it is. Do not do a sysOut
System.out.println(response.body().string()); // Instead of doing a sysOut return the value.
Or
Store the value of the response to a JSON then convert it to GSON and then return the value.
EDIT: Concerning Unicode characters. It turned out since my location is not an English-speaking country, the json i was accepting was not in English as well. I added this header:
.addHeader("Accept-Language", Locale.US.getLanguage())
to the request to fix that.
I am making http requests to my REST server. As a response I get the JSON body. But I need to get also a parameter from the response header, as I keep the user token in it. I have looked at a lot of posts on Stack Overflow for similar questions, but I they don't seem to have helped me. I want to make a JSON request, and get the body and headers form the response. How can I do it? Here is my code:
Please don't mark the question as duplicate, as I have not found any example where I can retrieve both: the response header and the response body. For the existing questions, users get only the headers
JsonObjectRequest req = new JsonObjectRequest(AppConfig.URL_LOGIN, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
hideDialog();
try {
JSONObject jObj = response;
String uid = jObj.getString("_id");
String name = jObj.getString("fullName");
String email = jObj.getString("email");
// Inserting row in users table
db.addUser(name, email, uid);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(
LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
String toastError = "Response code: " + networkResponse.statusCode;
Toast.makeText(getApplicationContext(),
toastError, Toast.LENGTH_LONG).show();
hideDialog();
}
});
You can't do that using JsonObjectRequest. You should extend the Request class and implement parseNetworkResponse() method which provides access to low-level NetworkResponse object. Downside is of course that you have to re-implement JSON parsing as well, but this is not really a big deal.
Something like:
public class HeaderAwareJsonRequest extends Request<Pair<JSONObject,Map>> {
protected Response.Listener<Pair<JSONObject,Map>> listener;
public HeaderAwareJsonRequest( int method, String url, Response.Listener<Pair<JSONObject,Map>> listener, Response.ErrorListener errorListener ) {
super( method, url, errorListener );
this.listener = listener;
}
#Override
protected Response<Pair<JSONObject,Map>> parseNetworkResponse( NetworkResponse response ) {
try{
String jsonString = new String( response.data, HttpHeaderParser.parseCharset( response.headers ) );
// pair contains the json body and headers
Pair pair = new Pair( new JSONObject( jsonString ), response.headers );
return Response.success( pair, HttpHeaderParser.parseCacheHeaders( response ) );
}catch( Exception e ){
return Response.error( new ParseError( e ) );
}
}
}
then in the calling code:
HeaderAwareJsonRequest req = new HeaderAwareJsonRequest(
Request.Method.GET,
AppConfig.URL_LOGIN,
new Response.Listener<Pair<JSONObject,Map>>() {
#Override
public void onResponse(Pair<JSONObject,Map> response) {
JSONObject jObj = response.first;
Map headers = response.second;
String someHeader = headers.get( "aaaaa" );
...
}
},
new Response.ErrorListener() {...}
);
I am trying to follow a online tutorial to create this login, but I receive this error. I tried this on localhost but it doesn't work on a server. Can anybody tell me what is my mistake please. Here's my code:
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
It means either the response is not in JSON format or the client side did not get any response at all. Try to following steps.:-
1.Before using the URL in the application check it in a web browser if you are getting the desired response or not. If there is any server side error it will be displayed in the web browser.
2. Now check the serve side response with a JSON validator to check if the response is a valid JSON or not
3.If your server side is then use logcat or toast message to print your response and check the response.
I just found out the way to solve it. It's the codes in the DB_Functions.php file that caused the problem. I have changed the code a bit then it works now. Thanks so much you guys for the help.I have also attached the code in case someone ran into the same problem. Good luck guys
public function getUserByEmailAndPassword($email, $password) {
$result = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
// check for result
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
$result = mysqli_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
I'm trying to run a query to search for locations using search query facebok graf api for android.
If I try to run this query in the Graph API Explorer, I get this result.
But programmatically it is impossible. I try to make so:
new GraphRequest(AccessToken.getCurrentAccessToken(),"/search?q=coffee&type=place¢er=37.76,-122.427",null,HttpMethod.GET,new GraphRequest.Callback()
{
public void onCompleted(GraphResponse response)
{
Log.v("HelloFacebook", response.toString());
}
}).executeAsync();
}
i get nothing (param distance must be number)
if I try without distance i get too nothing, but another message (An access token is required to request this resourse and too)
What could be the problem?
Right syntax for search in Facebook graph API:
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"/search",
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
// Insert your code here
}
});
Bundle parameters = new Bundle();
parameters.putString("type", "place");
parameters.putString("center", "53,27");
parameters.putString("distance", "30000");
request.setParameters(parameters);
request.executeAsync();
The syntax that Vadim Korzun provided did unfortunately not directly work for me. But it gave me some ideas. I wanted to search for people by names. Here the code I impelemted according to the newPlacesSearchRequest(...)-method of the official GraphRequest-class:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
Bundle parameters = new Bundle(2);
parameters.putString("type", "user");
parameters.putString("q", "Albert Einstein");
GraphRequest.Callback wrapper = new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
try {
JSONObject jso = response.getJSONObject();
JSONArray arr = jso.getJSONArray( "data" );
for (int i = 0; i < (arr.length()); i++) {
JSONObject json_obj = arr.getJSONObject(i);
// START: Your code goes here
String id = json_obj.getString("id");
String name = json_obj.getString("name");
// ...
// END: Your code goes here
}
} catch (JSONException e) {
Log.e("name: ", e.getMessage());
}
}
};
GraphRequest gr = new GraphRequest(accessToken, "search", parameters, HttpMethod.GET, wrapper);
gr.executeAsync();
hope this helps...