HTTP响应未提供输出

时间:2018-08-29 13:56:50

标签: java android api post httprequest

我正在研究与房间安全控制设备进行交互的应用程序。 我想从API获取设备信息。我正在使用 HttpUrlConnection POST方法。它打到API,我得到200 OK响应,但是我得到了

  

“ {” json“:{” control“:{” cmd“:” getdevice“,” uid“:256}}}}不存在”

我尝试了stackoverflow和其他平台上的所有解决方案,但没有给出输出。 此外,我已经在Postman上测试了此API,并且可以在其中工作并提供设备信息。

代码如下:

 public class HTTPRequestTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {

        String username = "admin";
        String password = "888888";
        URL url = null;
        try {
            url = new URL("http://192.168.100.25/network.cgi");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        assert url != null;
        HttpURLConnection httpRequest = null;
        try {
            httpRequest = (HttpURLConnection) url.openConnection();
            httpRequest.setRequestMethod("POST");
            httpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpRequest.setDoInput(true);
            httpRequest.setDoOutput(true);
      android.util.Base64.encode(authString.getBytes(), android.util.Base64.DEFAULT);
            httpRequest.addRequestProperty("Authorization", "Basic " + "YWRtaW46ODg4ODg4"); // This is auth bytecode
            httpRequest.connect();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    try {

        JSONObject json = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObjectControl = new JSONObject();
        jsonObjectControl.put("cmd","getdevice");
        jsonObjectControl.put("uid",256);
        jsonObject.put("control",jsonObjectControl);
        json.put("json", jsonObject);

        String encodedData = URLEncoder.encode( json.toString(), "UTF-8" );

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpRequest.getOutputStream()));
        writer.write(encodedData);
        writer.flush();

    BufferedReader bufferedReader = null;

        bufferedReader = new BufferedReader
                (new InputStreamReader(httpRequest.getInputStream(), "UTF-8"));

    String line = null;
    StringBuilder sb = new StringBuilder();

    do {
        line = bufferedReader.readLine();
        sb.append(line);
        Log.i("Output line: ",sb.toString());
    }
    while(bufferedReader.readLine()!=null);
    bufferedReader.close();
        int responseCode = httpRequest.getResponseCode();
        String resMsg = httpRequest.getResponseMessage();

    String result = sb.toString();
     Log.d("Output: ","--"+result);
        Log.d("Response Code: "+responseCode, "!!");
        Log.d("Response MSG ","--"+resMsg);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
        return null;
    }
}

0 个答案:

没有答案