【问题标题】:Android Volley ignoring params can't connectAndroid Volley忽略参数无法连接
【发布时间】:2014-06-23 14:46:34
【问题描述】:

我正在尝试使用 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;
    }

}

【问题讨论】:

  • 将 GET 更改为 Request.Method.Post
  • 意外响应代码 401

标签: java android http-headers httprequest android-volley


【解决方案1】:

您将 HTTP 请求类型指定为 GET,但添加了表单数据 给它。

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

你想要做一个 HTTP Post 方法。因此你应该更换 Request.Method.GET 到 Request.Method.POST 将 FormData 发布到 服务器。

更多关于 HTTP 方法here

编辑:如果您在尝试 HTACCESS 时收到 401,请参阅question。您需要使用 Authenticator 传递参数。

【讨论】:

  • 我已经尝试过了,仍然得到 Unexpected response code 401
  • 401 是“未授权”的状态码。这意味着您没有足够的权限访问此 url。 @192.168.1.1/represente-mais-api/api/clientes 正在运行什么代码?
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 2014-08-25
相关资源
最近更新 更多