任何人都可以用我當前的代碼來幫忙嗎?目前,我能夠從存儲在我的原始文件夾中的json文件中繪製和解析json數據,並將其顯示在android studio的可滾動textview中。但是,我希望直接從其他API中繪製數據,並將其顯示在列表視圖中。但是,我不確定這些代碼。任何幫助表示讚賞。謝謝。如何調用需要基本認證的rest api?
API的例子:
API:測試/ API /位置/ V2 /客戶
基本身份驗證:用戶名:test,密碼:測試
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
},
基於json的例子,我想檢索馬cAddress,ip地址,floorRefId和firstLocatedTime在我的列表視圖顯示它
我目前的代碼:
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());
}
}
您可以使用Retrofit庫與Web服務進行交互。它內在使用Gson庫來解析Json對象。例如: - http://www.vogella.com/tutorials/Retrofit/article.html –