我已经看过很多教程了,但是我被难住了。这是到目前为止我从几个指南中总结出来的代码:
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
try {
// Create URL
url = baseUrl + "signin?username=" + mEmail + "&password=" + mPassword + "&remember_me=true&accept_terms=true";
// Next, we create a new JsonArrayRequest. This will use Volley to make a HTTP request
// that expects a JSON Array Response.
// To fully understand this, I'd recommend readng the office docs: https://developer.android.com/training/volley/index.html
JsonArrayRequest arrReq = new JsonArrayRequest(Request.Method.GET, url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Check the length of our response
if (response.length() > 0) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObj = response.getJSONObject(i);
String repoName = jsonObj.get("name").toString();
String lastUpdated = jsonObj.get("updated_at").toString();
} catch (JSONException e) {
// If there is an error then output this to the logs.
Log.e("Volley", "Invalid JSON Object.");
}
}
} else {
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// If there a HTTP error then add a note to our repo list.
Log.e("Volley", error.toString());
}
}
);
// Add the request we just defined to our request queue.
// The request queue will automatically handle the request as soon as it can.
requestQueue.add(arrReq);
// Simulate network access.
Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}
for (String credential : DUMMY_CREDENTIALS) {
String[] pieces = credential.split(":");
if (pieces[0].equals(mEmail)) {
// Account exists, return true if the password matches.
return pieces[1].equals(mPassword);
}
}
// TODO: register the new account here.
return false;
}我尝试使用的接口是:https://neurofit.me/nfoidc/api/swagger-ui#/我想在我的应用程序中实现一个登录函数,所以我想调用登录函数。我有用户输入的电子邮件和密码,但还有其他参数,如accept-language和user-agent,称为headers。
我非常困惑如何才能只发送电子邮件和密码,并得到一个JSON文件。我也不认为我构造的URL是正确的。
https://stackoverflow.com/questions/50934216
复制相似问题