这是我的CustomRequest
代码。我正在使用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);
我创建了自定义请求助手类。使用此link。但它也不适合我。
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- 我的API代码
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);
}
响应正在传递
$json_response = json_encode($api_response);
// Deliver formatted data
echo $json_response;
但它给了我错误
com.android.volley.AuthFailureError
请帮忙
答案 0 :(得分:0)
终于找到了答案。
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);
在PHP中,我们将数据作为json获取。现在我们必须将json数据转换为php可读。
$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'];
我们通过解码json来获取所有数据。