想通过上海发布的API获取交通卡余额,但OkHttp收到不是json对象而是html代码

在**postman**中测试是可以获得json对象的,但是写在程序里就不可以了

图片说明

代码如下

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        Button submitNumber = (Button) view.findViewById(R.id.submit_number);
        EditText editText = (EditText) view.findViewById(R.id.sptc_number);

        final String sptcNumber = editText.getText().toString();
        submitNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequestWithOkHttp(sptcNumber);
            }
        });
        return view;
    }



    public void sendRequestWithOkHttp(final String cardNumber) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
//                    传入交通卡卡号
                    RequestBody requestBody = new FormBody.Builder()
                            .add("cardno", cardNumber)
//                            .add("arg1", cardNumber)
                            .add("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.1; AMOI N828 Build/JOP40D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.59 Mobile Safari/537.36")
                            .build();

                    Request request = new Request.Builder()
//                            .url("https://shanghaicity.openservice.kankanews.com/public/traffic/Jtkapi?cardno=" + cardNumber)
                            .url("https://shanghaicity.openservice.kankanews.com/public/traffic/Jtkapi")
//                            .url("http://www.sptcc.com/servlet/CardAmtServelet")
                            .removeHeader("User-Agent")
                            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.116")
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d(TAG, responseData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

另外就是使用http://www.sptcc.com/servlet/CardAmtServelet的话收不到回复或者超时,
但是在这两个接口在postman里测试也是没有问题的
图片说明
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问