如何调用需要基本身份验证的rest api?

时间:2017-02-20 15:53:26

标签: java android api android-studio

任何人都可以帮助解决我目前的代码吗?目前,我能够从存储在我的原始文件夹中的json文件中绘制和解析json数据,并将其显示在android studio中的可滚动文本视图中。但是,我希望直接从其余API绘制数据,并将其显示在listview中。但是,我不确定这些代码。任何帮助表示赞赏。谢谢。

API示例:

API:testing / api / location / v2 / clients

基本身份验证:用户名:test,密码: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示例,我希望检索MacAddress,Ipaddress,floorRefId和firstLocatedTime并在我的listview中显示它

我目前的代码:

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());
}

}

2 个答案:

答案 0 :(得分:0)

您可以使用改进来获取和解析来自api的json响应。

本教程将帮助您使用改造。

http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/

答案 1 :(得分:-1)

您可以将登录名/密码放入网址,如下所示:

http://user:password@api.stackoverflow.com/

相关问题