Alright, I've looked all over the website for a possible solution to send a POST request using android but I don't seem to understand the usage of Volley here or the method which was starting an async thread to make the request.
private RequestQueue signupRequestQ = Volley.newRequestQueue(this);
#Override
public void onClick(View view) {
String postUrl = "https://mypost.url";
signupRequestQ.start();
switch (view.getId()) {
case R.id.signupBtn:
if (((EditText) findViewById(R.id.passwordSignup)).getText().toString().length() < 6) {
Dialog.showInvalidInputFieldsDialog(this,"Password must be longer than 5 characters").show();
} else {
try {
StringRequest strRequest = new StringRequest(Request.Method.POST, postUrl,
response -> {
System.out.println(response);
},
error -> {
System.out.println("What the funk");
}) {
#Override
protected Map<String, String> getParams() {
LinkedHashMap<String, String> postForm = new LinkedHashMap<String, String>();
postForm.put("test", "var");
return postForm;
}
};
signupRequestQ.add(strRequest);
} catch (ParseException e) {
System.out.println("Something went wrong");
}
}
}
For some reason, this code causes my app to crash with a java.lang.RuntimeException and java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference.
However, I should not be encountering the NullPointerException as the Volley RequestQueue javadoc clearly stated that I only had to provide a context for the newRequestQueue method which I did.
Could anyone please help with my issue? Thank you!
It's because you instanciate your RequestQueue with a null Context at the top of your Activity :
private RequestQueue signupRequestQ = Volley.newRequestQueue(this); // "this" is null here
Try to create your RequestQueue in the onCreate() method of your Activity like :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
signupRequestQ = Volley.newRequestQueue(this);
}
Related
I can get very strange issue in my project. I can get the response from volley over the internet and after reposne I want to store it in sharedpref, but issue is that when I get the response and showed up within resonse function then it shows correct data, but when I used to save it outside the response function sharedpref it gives 0. I declared the string public and top of the class but got no luck. Am very strange whats the issue.
SharedPreferences savelogin = getSharedPreferences("login",MODE_PRIVATE);
final SharedPreferences.Editor slogin = savelogin.edit();
String url = "https://datafinderdatabase.xyz/dfapi/FetchId.php?username="+fuser;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
userid = response.toString();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toasty.error(getApplicationContext(), "Network Issue", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
slogin.putString("username",fuser);
slogin.putString("password",fpass);
slogin.putString("userid",userid);
slogin.commit();
This is because your call to the API is asynchronous. Therefore, the result you get back may be processed after that function finishes. To solve this, you can take a Interface approach, explained here for a similar issue:
wait until firebase retrieves data
OR: you need to save the variable to the shared preferences inside the onResponse function.
These:
slogin.putString("username",fuser);
slogin.putString("password",fpass);
slogin.putString("userid",userid);
slogin.commit();
must be inside this:
#Override
public void onResponse(String response) {
userid = response.toString();
//here
}
My java skills are quite good, still I fail to grasp what happens here.
I am programming an android app and right now I am programming a class which uses the Google Volley API in order to interact with a web-server.
The problem is that I want the class to be called like this:
Server_interaction s_i = new Server_interaction(getApplication);
String text = s_i.post_request();
Text should now contain the returned post request, in my case the string "Hello from server". Instead, it turns out to be null. This happens because the post_request method seems to return before having executed the post_request.
Here is the Server_interaction class:
public class Server_interaction
{
String server_url = "someipadress/greeting.php"; //this address is correct, but I want to hide it for you guys :)
String response_string;
Context myContext;
RequestQueue requestQueue;
public static final String TAG = Server_interaction.class.getSimpleName();
/* Here we add a constructor that takes in context. We need a context for volley requestqueue, and this is an elegant way*/
public Server_interaction(Context context)
{
myContext = context;
requestQueue = Volley.newRequestQueue(myContext);
}
public String post_request()
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response)
{
response_string = response;
requestQueue.stop();
Log.i(TAG, "the response is: "+ response_string);
}
}
, new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
response_string = "Something went wrong";
//error.printstacktrace()
requestQueue.stop();
}
}
); //stringrequest parameter end
//add request to requestqueue
requestQueue.add(stringRequest);
Log.i(TAG, "the response again:: "+ response_string);
return response_string;
}
}
When being executed, logcat shows this:
01-22 19:00:44.878 2954-2954/com.example.koenraad.Exigentia I/Server_interaction: the response again:: null
01-22 19:00:44.926 2954-2954/com.example.koenraad.Exigentia I/Server_interaction: the response is: hello from server
So this means that the string is null when being returned, then afterwards it is set. How can this be fixed?
Thanks in advance!
Your code is asynchrounous.
The StringRequest object created in method post_request() will be used by the framework after the method returns.
String name;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getData();
this.setText(name);
}
private void getData(){
name = object.getString("name");
}
I'm just making my line of code short, anyway the idea is already there. So on the method getData() I'm retrieving data from the web. I am able to test and was able to get the result however when I put the name on the onCreate() it gives me a null value. How to do some work arounds on this?
public class Testing extends NavigationLiveo{
String name;
#Override
public void onInt(Bundle savedInstanceState) {
getData();
Log.wtf("name0", name);
this.username.setText(name);
}
private void getData() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
try {
name = object.getString("name");
Log.wtf("name1", name);
} catch (Exception e) {
Log.wtf("testing1", e.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("user", user_pid);
return params;
}
};queue.add(sr);
}
}
Once again, I've made my coding simple. I can really get the data from the web server. It's just that whenever the I'll try to access the name on the onInt(Bundle savedInstanceState) it returns a null value. The log says it all as well. "name1" returns a value while "name0" returns null.
The only explanation is that the object.getString("name") returns null, or you are setting it to null somewhere afterwards. By the way, when retreiving data from the web, you should use AsyncTask, for more details refer here.
Can you put the getData() code?
I think that since the method uses AsyncTask or so to download the data from URL asynchronously. That means the method hasn't completed downloading, the setText(name) code already executed. So it displays null.
I have created an activity in which i insert some records into a mysql database. I declared a global variable named lastInsertId. When i try to println the variable inside the onResponse method, works fine but when i try to println outside the method returns null. I need to use this variable also outside the method. What can be done?
Here is my code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
System.out.println(lastInsertId); // get's null
}
Thanks!
They are different results because although you are firing your request to your local HTTP server, your last println fires BEFORE you set the lastInsertId.
You need to think multi-threaded. Your HTTP request is running in the background and the UI thread is continuing. So the order of execution is not in the order the code appears in your sample.
Request queue created
Request created
Request added to queue (presumably starting HTTP call also)
Prints out lastInsertId (null)
lastInsertId is set from your response
Prints out lastInsertId
user a setter method of your variable inside onResponse
This is happening because the lastInsertId is getting declared, but never initialized,
then the StringRequest request is using a callback through anonym interfaces to print the value, that is happening asynchronous way, but your code is going forward and tryinh to print the value before that happens.
you don net extra setters o getters, you need to validate / that the string is not empty, OR only print its value inside that callack.
I figure it out. I answered this question after about a year, beacuse i saw that this post had a few hundred visitor. Hope my answer will help other feature visitors to get data out from onResponse method. Here is the code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
callback.onSuccess(lastInsertId);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
}
public interface VolleyCallback{
void onSuccess(ArrayList<Data> dataArrayList);
}
And this is the code we need inside the Activity.
public void onResume(){
super.onResume();
getString(new VolleyCallback(){
#Override
public void onSuccess(String result){
System.out.println(result); // returns the value of lastInsertId
}
});
}
I want to get some JSON code from my API. I wish to do something like this in the OnCreate:
try
{
String jsonstr = getSomeThingAndPauseAndroid("http://someplace.com/api/xyz");
if (!jsonstr.isEmpty()) JSONObject jsonobj = new JSONObject(jsonstr);
}
catch { // handle error }
But when it happens, Android just go doing stuff and don't wait for the request to complete and response and I get nothing on jsonstr.
Is there some way to do that not needing a lot of new class files?
The correct way to do this is to make the request asynchronously and trigger a method on response. This is an example using Google's Volley:
//Start volley:
RequestQueue queue = Volley.newRequestQueue(this); // this = context
final String url = "http://someplace.com/api/xyz"";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
//This is where you setup your UI
//Remember to dismiss the ProgressDialog
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
//Remember to dismiss the ProgressDialog
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
//Here you should add a ProgressDialog so the user knows to wait
queue.add(getRequest);