[英]How to call rest api that require basic authentication?
could anyone help out with my current codes ? 谁能帮忙我当前的代码? Currently I am able to draw and parse the json data from a json file stored in my raw folder and display it in a scrollable textview in android studio.
目前,我能够从原始文件夹中存储的json文件中提取并解析json数据,并将其显示在android studio中的可滚动textview中。 However, I wish to draw the data directly from the rest API and display it in a listview instead.
但是,我希望直接从其余API提取数据,并将其显示在列表视图中。 However, I am unsure of the codes.
但是,我不确定代码。 Any help is appreciated.
任何帮助表示赞赏。 Thank you.
谢谢。
API Examples: API范例:
API: testing/api/location/v2/clients API:testing / api / location / v2 / clients
Basic Authentication: Username: test, Password: test 基本身份验证:用户名:test,密码:test
JSON EXAMPLE: JSON示例:
{
"macAddress": "00:00:2a:01:00:48",
"mapInfo": {
"mapHierarchyString": "CiscoCampus>Building 9>IDEAS!>CakeBread",
"floorRefId": "723413320329068650",
"floorDimension": {
"length": 74.1,
"width": 39,
"height": 15,
"offsetX": 0,
"offsetY": 0,
"unit": "FEET"
},
"image": {
"imageName": "domain_0_1462212406005.PNG",
"zoomLevel": 4,
"width": 568,
"height": 1080,
"size": 1080,
"maxResolution": 8,
"colorDepth": 8
},
"tagList": [
"test",
"Entrance",
"Restroom",
"Coffee & Snack",
"Men's Clothing",
"puma"
]
},
"mapCoordinate": {
"x": 17.934856,
"y": 23.121172,
"z": 0,
"unit": "FEET"
},
"currentlyTracked": true,
"confidenceFactor": 56,
"statistics": {
"currentServerTime": "2017-02-20T15:49:30.155+0000",
"firstLocatedTime": "2016-11-02T11:34:32.077+0000",
"lastLocatedTime": "2017-02-20T15:49:27.690+0000",
"maxDetectedRssi": {
"apMacAddress": "00:2b:01:00:0a:00",
"band": "IEEE_802_11_B",
"slot": 0,
"rssi": -44,
"antennaIndex": 0,
"lastHeardInSeconds": 3
}
},
"historyLogReason": null,
"geoCoordinate": null,
"networkStatus": "ACTIVE",
"changedOn": 1487605767690,
"ipAddress": [
"10.10.20.231"
],
"userName": "",
"ssId": "test",
"sourceTimestamp": null,
"band": "IEEE_802_11_B",
"apMacAddress": "00:2b:01:00:0a:00",
"dot11Status": "ASSOCIATED",
"manufacturer": "Trw",
"areaGlobalIdList": [
43,
3,
2,
1,
44,
27,
45,
58,
59,
60,
87,
81,
42,
111
],
"detectingControllers": "10.10.20.90",
"bytesSent": 154,
"bytesReceived": 140,
"guestUser": false
}, },
Based on the json example, i wish to retrieve the MacAddress, Ipaddress,floorRefId and firstLocatedTime and display it in my listview 基于json示例,我希望检索MacAddress,Ipaddress,floorRefId和firstLocatedTime并将其显示在我的列表视图中
My Current Codes: 我当前的代码:
public class AttendanceList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); //<< this
setContentView(R.layout.activity_attendance_list);
Bundle extras = getIntent().getExtras();
String ClassValue = extras.getString("ClassSpinnerValue");
String LessonValue = extras.getString("LessonSpinnerValue");
String LocationValue = extras.getString("LocationSpinnerValue");
final TextView classV = (TextView) findViewById(R.id.tv_class);
final TextView lessonV = (TextView) findViewById(R.id.tv_lesson);
final TextView locationV = (TextView) findViewById(R.id.tv_location);
classV.setText(ClassValue);
lessonV.setText(LessonValue);
locationV.setText(LocationValue);
Button SubmitAttendance = (Button) findViewById(R.id.btn_subattendance);
SubmitAttendance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AttendanceList.this, AttendanceMain.class);
Toast.makeText(AttendanceList.this,"Attendance Submitted",
Toast.LENGTH_SHORT).show();
startActivity(i);
}
});
}
public void loadGrades (View v) {
Resources res = getResources();
InputStream is = res.openRawResource(R.raw.student_device);
Scanner scanner = new Scanner(is);
StringBuilder builder = new StringBuilder();
while (scanner.hasNextLine()) {
builder.append(scanner.nextLine());
}
parseJson(builder.toString());
}
private void parseJson(String s) {
TextView txtDisplay =(TextView) findViewById(R.id.tv_display);
StringBuilder builder = new StringBuilder();
try {
JSONObject root = new JSONObject(s);
JSONObject student = root.getJSONObject("student-information");
JSONArray courses = student.getJSONArray("courses");
for (int i = 0; i < courses.length(); i++) {
JSONObject course = courses.getJSONObject(i);
builder.append(course.getInt("username"))
.append("")
.append(course.get("usernameunique"))
.append("\n");
builder.append("Location: ")
.append(course.getLong("floorRefId")).append("\n\n");;
}
}
catch (JSONException e) {
e.printStackTrace();
}
txtDisplay.setMovementMethod(new ScrollingMovementMethod());
txtDisplay.setText(builder.toString());
}
} }
You can use retrofit to get and parse json response from an api. 您可以使用改造来获取和解析来自api的json响应。
This tutorial will help you to use retrofit. 本教程将帮助您使用改造。
http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/ http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/
You can put you login/password into the url like this: 您可以像这样将登录名/密码放入URL:
http://user:password@api.stackoverflow.com/ http:// user:password@api.stackoverflow.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.