我使用授权令牌访问JSON api Web服务。我使用下面的代码尝试这样做,并在Q标题中返回此错误,因此有人可以帮我提供示例代码的分步解决方案:
以下是我致电的网络服务链接: http://api.nokhbatalatbaq.com/api/Packages/1/Comments
public class CommentsAsyncTask extends AsyncTask<String, Void, String> {
HttpURLConnection httpcon;
String url = null;
String data = null;
String results = null;
Activity a ;
private final WeakReference<Activity> activityWeakRef;
Throwable error;
public CommentsAsyncTask(Activity context,String url) {
this.activityWeakRef = new WeakReference<Activity>(context);
this.a=context;
this.url=url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://api.nokhbatalatbaq.com/api/Packages/1/Comments" );
request.setHeader("Content-type", "application/x-www-form-urlencoded");
String requestBody ="access_token=<OMITTED>&username=<OMITTED>&userID=3&issued=Sun, 14 Feb 2016 05:31:18 GMT&expires=Sun, 28 Feb 2016 05:31:18 GMT";
try {
request.setEntity(new StringEntity(requestBody));
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
/* Checking response */
try {
HttpResponse response = client.execute(request);
results = "ERROR";
results = EntityUtils.toString(response.getEntity());
Log.d("STACK", "Response::" + results);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return results;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("error", result);
Toast.makeText(a, result,
Toast.LENGTH_LONG).show();
}