簡體   English   中英

Android Volley忽略參數無法連接

[英]Android Volley ignoring params can't connect

我正在嘗試使用Volley連接到本地API,我傳遞了所有必需的參數,但是如果我擺脫了電子郵件和密碼,僅讓令牌請求起作用,它就無法工作,所以問題出在電子郵件和密碼,但是那些參數被忽略了,我如何使它工作?

package quest.testvolley;

import com.android.volley.AuthFailureError;
import com.android.volley.VolleyLog;
import com.kpbird.volleytest.R;



        import org.json.JSONObject;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.View;
        import android.widget.TextView;

        import com.android.volley.Request;
        import com.android.volley.RequestQueue;
        import com.android.volley.Response;
        import com.android.volley.VolleyError;
        import com.android.volley.toolbox.JsonObjectRequest;
        import com.android.volley.toolbox.Volley;

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

public class MainActivity extends Activity {

    private TextView txtDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtDisplay = (TextView) findViewById(R.id.txtDisplay);

        RequestQueue queue = Volley.newRequestQueue(this);


        String url = "http://192.168.1.1/represente-mais-api/api/clientes";

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("email", "rm@sss.com.br");
        params.put("senha", "sss");
        //params.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0afd19MfacGa3FFm8CM1hG0eDiIk8");




        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,
                url, new JSONObject(params),
                new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                txtDisplay.setText("Response => "+response.toString());
                findViewById(R.id.progressBar1).setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {



                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d( "Error: " + error.getMessage());


                }
            })



        {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();


                    headers.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                    return headers;
                }



        };
        queue.add(req);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

您將HTTP請求類型指定為GET,但尚未向其中添加表單數據。

  JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, ... 

您要執行HTTP Post方法。 因此,您應該將Request.Method.GET替換為Request.Method.POST以將FormData發布到服務器。

更多關於HTTP方法在這里

編輯:如果您在嘗試HTACCESS時得到401,請參閱此問題 您需要使用身份驗證器傳遞參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM