我是Android的新手,在我的应用中,我使用Volley集成WebServices,我必须将AccessToken
发送到服务器,并通过标头将其传递到服务器,但是当我运行我的应用程序时,我得到了ParseError
。请帮忙。
MainActivity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
makeJsonObjReq();
}
private void makeJsonObjReq() {
JSONObject request = new JSONObject();
try {
request.put("TripStatus", "-1");
request.put("MedicaidId", "10251025");
} catch (JSONException e) {
e.printStackTrace();
}
BackGroundPostServiceClass.makeJsonObjeRequest(MainActivity.this, request, "My url");
}
BackGroundPostServiceClass :
public class BackGroundPostServiceClass {
public static GetBatchDetails getProductDetailsImpl;
public static ProgressDialog pd;
public static void makeJsonObjeRequest(
final Context context, JSONObject jsonObject, String url) {
getProductDetailsImpl = ((GetBatchDetails) context);
pd = ProgressDialog.show(context, "Alert", "Please Wait...");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pd.dismiss();
getProductDetailsImpl.getBatchDetails(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
showSignOutAlertDialog(context, "TimeoutError");
} else if (error instanceof AuthFailureError) {
showSignOutAlertDialog(context, "AuthFailureError");
} else if (error instanceof ServerError) {
showSignOutAlertDialog(context, "ServerError");
} else if (error instanceof NetworkError) {
showSignOutAlertDialog(context, "NetworkError");
} else if (error instanceof ParseError) {
showSignOutAlertDialog(context, "ParseError");
}
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
String accesstoken = "To6k4mxO0rqvhPVSeZZ5Jr8Ish35vwSUoaxjbQZNgNz2qTd3PO0M4yFPmMCX4B-wdgkLz-DBf-EuMUyQLQDld0inlT3Rm_yc3N4S4Kgjz9buLkHjN_CBxKbaZPaKmP4cxrzqA1KXSbik11OidFVZg1f8m9Ygnra_WL_2hzZE-p1VMDZoj2z0ojofHoVvfnRXv3UTfmlFuHBhCTWKtT2SSGYjwHKEjYqhtjiMhbZ91B8";
headers.put("Authorization", "Bearer " + accesstoken);
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
int mStatusCode = response.statusCode;
System.out.println("Status code is===>"+mStatusCode);
return super.parseNetworkResponse(response);
}
};
jsonObjReq.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 150000;
}
@Override
public int getCurrentRetryCount() {
return 150000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}
AppController的:
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
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 ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
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);
}
}
答案 0 :(得分:2)
作为日志和BNK说响应很好。你遇到了问题,因为你可能没有收到json对象但是没有其他东西。
要进一步调试,您可以添加
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
System.out.println("Response is===>"+jsonString);
旁边的
System.out.println(“状态代码为===&gt;”+ mStatusCode);