【问题标题】:Android API Response handlingAndroid API 响应处理
【发布时间】:2023-03-30 11:57:02
【问题描述】:

我想在我的 Android 应用程序中处理来自我的 API 的 json 响应,但我找不到很多关于如何做到这一点的教程,有没有人有一些提示?

安卓代码:

                    URL url = new URL("http://thelinktomyapi.com");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    Log.i("JSON", jsonObj.toString());
                    DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                    //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                    os.writeBytes(jsonObj.toString());

                    os.flush();
                    os.close();

                    Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                    Log.i("MSG", conn.getResponseMessage());

                    conn.disconnect();

响应我烧瓶代码的一部分(非常简单)

respdata = {
        'imgurl2': 'google.nl'
    }
    resp = jsonify(respdata)
    resp.status_code = 200
    return resp

【问题讨论】:

  • 那些真的不适合我。我试过了
  • 这是您的有效网址吗:“thelinktomyapi.com”...?

标签: android


【解决方案1】:

试试这个我认为这会有所帮助

                URL url = new URL("http://thelinktomyapi.com");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept", "application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);

                Log.i("JSON", jsonObj.toString());
                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                os.writeBytes(jsonObj.toString());

                os.flush();
                os.close();

                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());


               DataInputStream inputStream = new DataInputStream(conn.getInputStream());
               BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
               StringBuilder result = new StringBuilder();
               String line = "";
               while((line = reader.readLine()) != null) {
                     result.append(line);
               }



                conn.disconnect();

感谢https://stackoverflow.com/a/9856272/5726600

【讨论】:

    【解决方案2】:

    试试,

    String response = "";
            try {
                URL url = new URL(reqUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    
                BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                String output;
                while ((output = br.readLine()) != null) {
                    response = response + output;
                }
                conn.disconnect();
            } catch (MalformedURLException e) {
                Log.e(TAG, "MalformedURLException: " + e.getMessage());
            } catch (ProtocolException e) {
                Log.e(TAG, "ProtocolException: " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "IOException: " + e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
    

    您将在 response 变量中得到您的回复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2019-06-13
      • 2021-04-23
      相关资源
      最近更新 更多