使用截击自定义发布请求并收到错误

This is my CustomRequest code. I am passing the parameters to post using Map.

Map<String, String> params = new HashMap<String, String>();
    params.put("name", username);
    params.put("buildingname", userbuildingname);
    params.put("area", userarea);
    params.put("city", usercity);
    params.put("mobileno", usermobileno);
    params.put("email", useremail);
    params.put("confirmpassword", userconfirmpassword);
    params.put("method", method);
    params.put("format", format);


    CustomRequest jsonObjReq = new CustomRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    JSONObject resp = response;
                    abc = resp.toString();
                }
            }, new Response.ErrorListener() {
                 @Override
                 public void onErrorResponse(VolleyError error) {
                     abc = error.getMessage();

                     Log.d(TAG, "Error: " + error.getMessage());
                 }
            });
    AppController.getInstance().addToRequestQueue(jsonObjReq);

And I created the custom request helper class. Using this link. But it also not works for me.

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject>{

private Listener<JSONObject> listener;
private Map<String, String> params;

public CustomRequest(String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

public CustomRequest(int method, String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
    return params;
};

@Override
protected void deliverResponse(JSONObject response) {
    listener.onResponse(response);
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

}

PHP- My API Code

else if(strcasecmp($_POST['method'],'userRegistration') == 0){
    $name = $_POST['name'];
    $buildingname= $_POST['buildingname'];
    $area= $_POST['area'];
    $city= $_POST['city'];
    $mobileno= $_POST['mobileno'];
    $email= $_POST['email'];
    $password= $_POST['confirmpassword'];
    deliver_response($_POST['format'],$response,false);
}

Response is passing like this

$json_response = json_encode($api_response);

    // Deliver formatted data
    echo $json_response;

But it gives me the error

com.android.volley.AuthFailureError

Please help

查看全部
doukun5339
doukun5339
2015/10/12 10:12
  • java
  • android
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复