自定义Gson请求和回复方法

时间:2018-11-02 07:40:49

标签: android android-volley gson

我做错了什么?我该如何解决?请解释为什么会发生此错误。如果运行成功,我的回应是什么?

MainActivity:

public class MainActivity extends AppCompatActivity {

String url = "http://vsstechnology.com/DTSignUp/api/business/create_business.php";
String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

    HashMap<String, String> params = new HashMap<>();
    params.put("address", "xxx");
    params.put("business_description", "xx");
    params.put("businessEmail", "xxx@gmail.com");
    params.put("business_name", "xxx");
    params.put("phone", "1234567890");
    params.put("business_type", "xx");
    params.put("created_by", "xxx");
    params.put("customer_name", "xxx");

    params.put("email", "yyy@gmail.com");
    params.put("firstName", "yyy");
    params.put("lastName", "yyy");
    params.put("phone", "6544324569");


    GsonRequest<Model> gsonRequest = new GsonRequest<Model>(Request.Method.POST, url, Model.class, params, new Response.Listener<Model>() {
        @Override
        public void onResponse(Model response) {
            Toast.makeText(getApplicationContext(), "success" + " " + response, Toast.LENGTH_LONG).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "error" + " " + error, Toast.LENGTH_LONG).show();
        }
    });

    requestQueue.add(gsonRequest);
}
}

型号:

public class Model {
private Business business;

private Contacts[] contacts;

private ServerResponse serverResponse;

public Model(Business business, Contacts[] contacts, ServerResponse serverResponse) {
    this.business = business;
    this.contacts = contacts;
    this.serverResponse = serverResponse;

}

public Business getBusiness() {
    return business;
}

public void setBusiness(Business business) {
    this.business = business;
}

public Contacts[] getContacts() {
    return contacts;
}

public void setContacts(Contacts[] contacts) {
    this.contacts = contacts;
}

public ServerResponse getServerResponse() {
    return serverResponse;
}

public void setServerResponse(ServerResponse serverResponse) {
    this.serverResponse = serverResponse;
}
}

自定义GsonRequest:

public class GsonRequest<T> extends Request<T> {
private Gson gson = new Gson();
private Class<T> clazz;
private Map<String, String> headers;
private Map<String, String> params;
private Response.Listener<T> listener;


/**
 * Make a GET request and return a parsed object from JSON.
 *
 * @param url    URL of the request to make
 * @param clazz  Relevant class object, for Gson's reflection
 * @param params Map of request headers
 */
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> params,
                   Response.Listener<T> listener, Response.ErrorListener errorListener) {
    super(method, url, errorListener);
    this.clazz = clazz;
    this.headers = null;
    this.params = params;
    this.listener = listener;
    gson = new Gson();
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();
}

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    return super.getParams();
}

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

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(
                gson.fromJson(json, clazz),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}
}

错误:

  

com.android.volley.ParseError:com.Google.gson.JsonSyntaxException:   java.lang.illegalStateException:应为BEGIN_OBJECT,但为STRING   在第1行第1列的路径$

1 个答案:

答案 0 :(得分:0)

根据您的错误,您的API出了点问题,您可以使用Postman对其进行调试。

相关问题