我以前从未处理过这样的请求,而且我不知道在互联网上搜索什么... 所以我有这个任务,我有一个api url - https://www.myurl.com/oauth2/2.0/signin 它应该提供一个电子邮件和密码。网址接受post方法(我有点不知道这意味着什么),如果成功,应提供JSON对象,但我不知道如何在链接中提供电子邮件和密码。 。 我有这个AsyncTask
public class JSONGet extends AsyncTask<String, Void, String> {
String adres;
JSONObject jsonGet;
JSONParserCallback delegate;
public JSONGet(JSONParserCallback callback, String adres) {
this.delegate = callback;
this.adres = adres;
}
@Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("testemail@mail.uk", "android"));
HttpPost httppost = new HttpPost("https://www.myurl.com/oauth2/2.0/signin");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse hr = client.execute(httppost);
int status = hr.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity he = hr.getEntity();
String data = EntityUtils.toString(he);
jsonGet = new JSONObject(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
delegate.onFinishedParsing(jsonGet);
}
}
答案 0 :(得分:3)
您可以提供如下用户名和密码:
nameValuePairs.add(new BasicNameValuePair("userName","{username string}"));
nameValuePairs.add(new BasicNameValuePair("password","{password string}"));
将{username string} and {password string}
替换为您的用户名和密码。