带参数的Android Volley URL

时间:2017-06-22 06:48:55

标签: android django url android-volley

我使用排球库(GET)编写了代码,我必须从Django的其余API中获取数据。

实际上问题是我在登录时获得了用户ID,我只是通过共享首选项保存了该ID,我必须在URL中传递ID以获取钱包详细信息但我收到错误

BasicNetwork.performRequest:意外的响应代码403

public class Wallet extends AppCompatActivity {

TextView all_tv1;
Button credit_bt, debit_bt;
SharedPreferences pref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_wallet);
    pref=getApplication().getSharedPreferences("Options", MODE_PRIVATE);
    Integer Key_UserID=pref.getInt("UserId_Value", 0);
   // String a = Key_UserID.toString();

/ Json请求 /         String uri =“http://staging.exol.in/wallet/wallet/api/wallet-detail/+Key_UserID”;         RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, uri, null,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    Toast.makeText(getApplicationContext(),"stop",LENGTH_SHORT).show();
                    //    String data = "";
                    try {
                        for(int i=0; i< response.length(); i++) {
                            JSONObject obj = response.getJSONObject(i);
                            String tnx_id = obj.getString("tnx_id");
                            String transition_type = obj.getString("transition_type");
                           // String email = obj.getString("email");
                          //  System.out.print("\nggtrgtg"+user+"\ngfg"+amount);

                            Toast.makeText(getApplicationContext(),tnx_id +  transition_type ,LENGTH_SHORT).show();
                            //    data = "" + id + "" + name + "" + email + "";
                            //txtDisplay.setText(txtDisplay.getText()+"\n" + id + "" + name + "\n" + email + "");
                            // Adds the data string to the TextView "results"
                        }
                    }



                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        e.printStackTrace();
                    }


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),"Faled Misreably" ,LENGTH_SHORT).show();
                }
            });
    //add request to queue
    requestQueue.add(jsonArrayRequest);






    setTitle("Wallet Window");
   Toolbar toolbar1 = (Toolbar)findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar1);











    all_tv1 = (TextView)findViewById(R.id.all_tv);
    all_tv1.setMovementMethod(new ScrollingMovementMethod());



    credit_bt = (Button)findViewById(R.id.credit_details1__bt);
    debit_bt = (Button) findViewById(R.id.debit_details1__bt);


    credit_bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Credit Tab", LENGTH_SHORT).show();
            Intent i = new Intent(getApplicationContext(), Wallet_Credit.class);
            startActivity(i);
            finish();
        }
    });



    debit_bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Debit Tab", LENGTH_SHORT).show();
            Intent i = new Intent(getApplicationContext(), Wallet_Debit.class);
            startActivity(i);
            finish();
        }
    });



}

}

1 个答案:

答案 0 :(得分:0)

你的uri字符串设置不正确(请注意结束&#34;)。而不是

String uri = "http://staging.exol.in/wallet/wallet/api/wallet-detail/+Key_UserID";

应该是

String uri = "http://staging.exol.in/wallet/wallet/api/wallet-detail/"+Key_UserID;