Android Volley - No Response from StringRequest - android

I'm unable to get JSON Response when using Android Volley. No Error also No Succesfull response see (Log.d("logr=",_response);). I use StringRequest to get JSON text from Google Map API Android V2. Here're the code
private String urla = "https://maps.googleapis.com/maps/api/place/search/json?location=";
private String urlb = "&types=hotel&radius=500&sensor=false&key=YOUR_KEY";
#Override //::LocationListener (Interface)
public void onLocationChanged(Location location) {
//Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), 13);
googleMap.animateCamera(cameraUpdate);
double x = location.getLatitude();
double y = location.getLongitude();
String request = urla+x+","+y+urlb;
Log.d("log1=",request);
StringRequest stringRequest = new StringRequest(Request.Method.GET, request,
new Response.Listener<String>() {
#Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
Toast.makeText(activity, "Update location user ==>" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "Current Location :" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude());
}
Any Solution to this problem ?

Where do you actually excecute the request? You have to add() it to Volley's RequestQueue, otherwise the request does not get send at all.
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = urla + x + "," + y + urlb;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
//excecute your request
queue.add(stringRequest);
See more here: Learn volley

Try this:
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
final StringRequest stringRequest = new StringRequest(Request.Method.POST, urlString, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
strr = response;
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
Log.e("Volley", "ERROR");
}
});
requestQueue.add(stringRequest);

Related

Get location from Facebook Account API

I've made lots of attempts to get the city for this account to view the account on the app but it dont work
//Login Facebook
public void Login()
{
Dialog dialog = new Dialog(Home.this);
dialog.setContentView(R.layout.customdialog_login);
login_button = (LoginButton)dialog.findViewById(R.id.login_button);
login_button.setReadPermissions("public_profile", "email", "user_birthday", "user_friends","user_location");
callbackManager = CallbackManager.Factory.create();
dialog.show();
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(final JSONObject object, GraphResponse response) {
try {
SaveSystem.SaveStringData("userid" , object.getString("id"));
SaveSystem.SaveStringData("first_name" , object.getString("first_name"));
SaveSystem.SaveStringData("last_name" , object.getString("last_name"));
SaveSystem.SaveStringData("gender" , object.getString("gender"));
SaveSystem.SaveStringData("admin" , "0");
SaveSystem.SaveStringData("ban" , "0");
SaveSystem.SaveStringData("type" , "facebook");
//CheckUser
final RequestQueue requestQueue = Volley.newRequestQueue(Home.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, Information.URL_CheckUsers + SaveSystem.LoadStringData("userid"), new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Toast.makeText(Home.this, "Location " + object.getJSONObject("location").getJSONObject("location").getString("city"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
if(response.equals("0"))
{
//Add Account
String URL_AddUser = Information.URL_AddUsers + SaveSystem.LoadStringData("userid") + "&username=" + SaveSystem.LoadStringData("first_name") + " " + SaveSystem.LoadStringData("last_name") + "&gender=" + SaveSystem.LoadStringData("gender") + "&admin=" + SaveSystem.LoadStringData("admin") + "&ban=" + SaveSystem.LoadStringData("ban") + "&type=" + SaveSystem.LoadStringData("type");
StringRequest stringRequest1 = new StringRequest(Request.Method.GET, URL_AddUser , new Response.Listener<String>() {
#Override
public void onResponse(String response) {
setAccountData();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Home.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest1);
}
else
{
setAccountData();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location{location}");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
here I try to present the area to experience
try {
Toast.makeText(Home.this, "Location " + object.getJSONObject("location").getJSONObject("location").getString("city"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
can I view Json facebook on Chrome ?? , I do not know how to arrange Json on facebook
I searched a lot but I dont get the city. Is it a problem in my blades or what please help me really thank you

Question marks in my data base with volley

I have a problem with old devices , when the data is sent to the server using an old device api 15 + 16 + 17 in Spanish or Portuguese, question marks will appear, and the new devices have no problems
txt_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edit_post.length() > 0)
{
txt_ok.setVisibility(View.GONE);
pd_newpostloading.setVisibility(View.VISIBLE);
String url = Information.URL_AddPendingPosts + Information.userid + "&authorname=" + Information.username + "&message=" + edit_post.getText().toString() + "&type=" + edit_category.getText().toString() + "&date=" + nowAsString;
try {
encodedValue = URLEncoder.encode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(ShowMessages.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, encodedValue, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
txt_ok.setVisibility(View.VISIBLE);
pd_newpostloading.setVisibility(View.GONE);
Toast.makeText(ShowMessages.this, "Done", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
txt_ok.setVisibility(View.VISIBLE);
pd_newpostloading.setVisibility(View.GONE);
Toast.makeText(ShowMessages.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
else
{
Toast.makeText(ShowMessages.this, "Empty", Toast.LENGTH_SHORT).show();
}
}
});
I hope to get some help thanks in advance :)

how to pass two Strings in "StringRequest" in android

i want to get data from two different tables in MYSQL so i used "StringRequest" method to retrieve data from MYSQL, In the below code i can get data from one table and view it in ListView in android but how i can change my code so that i will get data from another table too.
here is my code:
String url ="http://alwaysready.16mb.com/OnlineJobSort.php;";
String url_lock="http://alwaysready.16mb.com/LocalSort.php?";
StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
From the above code i can able to get data from "url" but i need to get data from both "url"&"url_lock".
Well, it's simple. You just need to make 2 StringRequests with different URL parameter
String url = "http://alwaysready.16mb.com/OnlineJobSort.php;";
String url_lock = "http://alwaysready.16mb.com/LocalSort.php?";
StringRequest stringRequest1 = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
StringRequest stringRequest2 = new StringRequest(url_lock, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest1);
requestQueue.add(stringRequest2);

Volley getting an error response

I am sending some post data using volley. Here is my code . It's a snippet from background service. Let me know if you want other files also. I've added the internet permission.
public void onLocationChanged(final Location location) {
mCurrentLocation = location;
pref.setLocation(location);
String url = getResources().getString(R.string.hostname); // it's a normal http
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Location sent", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
String errorMessage = "Err - data:"+error.getMessage();
Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
}
})
{
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERID,pref.getUserId());
params.put(KEY_LAT,String.valueOf(location.getLatitude()));
params.put(KEY_LNG,String.valueOf(location.getLongitude()));
return params;
}
};
queue.add(stringRequest);
}
Actually i saw that the one of the param UserId was having the Null value, That was creating the problem

Volley pass a response outside of the class

I want to pass a response outside of my classes (many classes)
public static void userLocation()
{
RequestQueue queue = Volley.newRequestQueue(context);
String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";
StringRequest dr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
//Toast.makeText(context, ""+response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error.
// Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
Log.d("error", ""+error);
}
}
);
queue.add(dr);
}
Please suggest how I can pass a response outside of the class
In volly its difficult to return response outside the class.because request on server run in background and if we return value followed by queue.add(url) then it will return null.So there is no solution till now.Thanks!!

Categories

Resources