简体   繁体   中英

Variable URL for a simple Volley string request

I'm creating a simple database using the volley library. im having problems selecting which specific database to send it to. Before I explain what my specific problem is here is the code:

package com.example.joker.sqltrial;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/*  *
 * Created by Joker on 2016-12-13.
 */

public class PostRequest extends StringRequest {


    private static String REGISTER_REQUEST_URL="";
    private Map<String, String> params;



    public PostRequest(String animal, int sizeClass, String spType, String          description, Response.Listener<String> listener){
        super (Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("animal", animal);
        params.put("size class", sizeClass+"");
        params.put("specie type", spType);
        params.put("description", description);
    }

    @Override
    public Map<String, String> getParams() {

        return params;
    }

    public static void urlSet(String URL){
        REGISTER_REQUEST_URL=URL;
    }
}

so in this line of code "super (Method.POST, REGISTER_REQUEST_URL, listener, null);", im providing a URL thats supposed to be a Final String...i would like to change that URL(the table/database) which it would go to, depending on what the user enters. in another class i have the URL selected, dependent on what is chosen from a spinner, and then that url is passed throw another method which passed it to this class using an object. here is the code for the other class

   btButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String animal = etAnimal.getText().toString();
                final String sizeClass = etsizeClass.getText().toString();
                final int spType  = Integer.parseInt(etspType.getText().toString());
                final int blaha = Integer.parseInt(etBlah.getText().toString());
                final String blahBlah = etBlahBlah.getText().toString();


                switch (category) {
                    case "human":
                        tempURL ="SOME SPECIFIC URL";
                        setURL(tempURL);
                        break;
                    case "alien":
                        tempURL ="SOME SPECIFIC URL";
                        setURL(tempURL);
                        break;
                    case "animal":
                        tempURL ="SOME SPECIFIC URL";
                        setURL(tempURL);
                        break;
                    default:
                        tempURL=null;
                        Toast.makeText(getBaseContext(), "try again and input correct characters", Toast.LENGTH_LONG).show();
                }


                Response.Listener<String> responseListener = new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            if (success){
                                Intent i = new Intent(MainActivity.this, ViewAd.class);
                                MainActivity.this.startActivity(i);
                            }
                            else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                builder.setMessage("Posting failed!")
                                        .setNegativeButton("retry", null)
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                };


                PostRequest postRequest = new PostRequest(animal, sizeClass, spType, description, responseListener);
                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                queue.add(postRequest);
            }
        });
    }


    public void setURL(String URL){
        tempURL=URL;
        PostRequest.urlSet(URL);


    }

...so essentially when the button is clicked, the response listener would see what was picked from the spinner and then depending on that pass on a specific URL which would set the URL in the other class before it is even called upon...what am i doing wrong? i know its a simple solution i cant figure it out. Please can somebody help me out? thank you.

and this is the stack trace i got:

01-19 21:06:20.300 221-542/? W/genymotion_audio: out_write() limiting sleep time 46802 to 39909
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:159)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:172)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.example.joker.sqltrial.MainActivity$1$1.onResponse(MainActivity.java:74)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.example.joker.sqltrial.MainActivity$1$1.onResponse(MainActivity.java:70)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at android.os.Looper.loop(Looper.java:136)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5001)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err:     at dalvik.system.NativeStart.main(Native Method)
01-19 21:06:22.620 221-542/? W/genymotion_audio: out_write() limiting sleep time 43809 to 39909
public static void urlSet(String URL){
    REGISTER_REQUEST_URL=URL;
}

By making this method and REGISTER_REQUEST_URL static, you are saying that every PostRequest object has the same URL. Since you want to change the URL for each request, neither this method nor the variable should be static. Instead, they should be a non-static member field and method.

This change will cause some other issues with your code. You will need to figure out how to set the URL on the correct PostRequest instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM