doukun5339 2015-10-12 10:12
浏览 31
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • duanduanxi9441 2015-12-31 10:46
    关注

    Finally found the answer.

    JSONObject params = new JSONObject();
            try {
                params.put("method", method);
                params.put("format", format);
                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);
            } catch (Exception e) {
    
            }
            JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, URL, params,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            VolleyLog.v("Response:%n %s", response.toString());
                            SignUp signUp = new SignUp();
                            try {
                                returnResponse(response.getString("saveUsersDetailsResponse"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.e("Error: ", error.getMessage());
                }
            }
            );
            AppController.getInstance().addToRequestQueue(req);
    

    And in PHP we get the the data as json. Now we have to convert the json data to php readable.

    $checkmethod = $_SERVER['REQUEST_METHOD'];
    $var = file_get_contents("php://input");
    $string = json_decode($var, TRUE);
    $method = $string['method'];
    $name = $string['name'];
    $email = $string['email'];
    $mobileno = $string['mobileno'];
    

    We get all data by decoding json.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题