Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android截取JSONObjectRequest返回401错误_Java_Android_Post_Android Volley_Jsonobject - Fatal编程技术网

Java android截取JSONObjectRequest返回401错误

Java android截取JSONObjectRequest返回401错误,java,android,post,android-volley,jsonobject,Java,Android,Post,Android Volley,Jsonobject,我正在尝试向服务器发送带有参数的POST请求。但是post参数总是空的 我尝试了一些来自stackoverflow的解决方案,但没有成功 我得到11.urlname的意外响应代码401 Map<String, String> jsonParams = new HashMap<String, String>(); jsonParams.put("username", "test@mail.com"); jsonParams.put("usertype", "userType

我正在尝试向服务器发送带有参数的POST请求。但是post参数总是空的

我尝试了一些来自stackoverflow的解决方案,但没有成功

我得到11.urlname的意外响应代码401

Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("username", "test@mail.com");
jsonParams.put("usertype", "userType");
jsonParams.put("apikey", "key");

JsonObjectRequest myRequest = new JsonObjectRequest(Request.Method.POST,apiURL, new JSONObject(jsonParams),

    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                String status=response.getString("status");

                if (status.equals("success"))
                {
                    txtResponse.setText("Valid user");
                }
                else {
                    txtResponse.setText("InValid USer");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=UTF-8");
        return headers;
    }
};
AppController.getInstance().addToRequestQueue(myRequest, "tag");
Map jsonParams=newhashmap();
jsonParams.put(“用户名”test@mail.com");
jsonParams.put(“usertype”、“usertype”);
jsonParams.put(“apikey”、“key”);
JsonObjectRequest myRequest=新的JsonObjectRequest(Request.Method.POST、APIRL、新的JSONObject(jsonParams),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
字符串状态=response.getString(“状态”);
如果(状态等于(“成功”))
{
setText(“有效用户”);
}
否则{
setText(“无效用户”);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=UTF-8”);
返回标题;
}
};
AppController.getInstance().addToRequestQueue(myRequest,“标记”);
我的应用程序控制器代码是

public class AppController extends Application {
    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class.getSimpleName();
私有请求队列mRequestQueue;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共无效addToRequestQueue(请求请求,字符串标记){
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}

如果您的问题尚未解决,您可以参考以下代码来构建请求主体(params)

然后

    Map<String, String> stringMap = new HashMap<>();    
    stringMap.put("username", "yourusername");
    stringMap.put("password", "yourpassword");
    ...
    String requestBody = buildRequestBody(stringMap);
Map stringMap=newhashmap();
stringMap.put(“用户名”、“您的用户名”);
stringMap.put(“密码”、“您的密码”);
...
String requestBody=buildRequestBody(stringMap);

尝试使用StringRequest…甚至我也遇到过同样的问题…使用StringRequest解决了它我尝试了
StringRequest
它不起作用
    Map<String, String> stringMap = new HashMap<>();    
    stringMap.put("username", "yourusername");
    stringMap.put("password", "yourpassword");
    ...
    String requestBody = buildRequestBody(stringMap);