Deprecated methods, how i can replace it with new

前端 未结 1 1556
太阳男子
太阳男子 2020-12-20 23:11

I have a problem with some deprecated methods, in particular with this that i posted here.

What that i want is simple: send data to server using http post ---> make

相关标签:
1条回答
  • 2020-12-20 23:52

    I think you can refer to my following sample code, then use its logic for your app:

    private class POSTRequest extends AsyncTask<Void, Void, String> {
    
            @Override
            protected String doInBackground(Void... voids) {
                String address = "http://192.16.1.100:24780/api/token";
                HttpURLConnection urlConnection;
                String requestBody;
                Uri.Builder builder = new Uri.Builder();
                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("grant_type", "password");
                stringMap.put("username", "bnk");
                stringMap.put("password", "bnkpwd");
    
                Iterator entries = stringMap.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = (Map.Entry) entries.next();
                    builder.appendQueryParameter(entry.getKey().toString(), entry.getValue().toString());
                    entries.remove();
                }
                requestBody = builder.build().getEncodedQuery();
    
                try {
                    URL url = new URL(address);
                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    // urlConnection.setRequestMethod("POST");
                    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                    writer.write(requestBody);
                    writer.flush();
                    writer.close();
                    outputStream.close();
    
                    JSONObject jsonObject = new JSONObject();
                    InputStream inputStream;
                    // get stream
                    if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
                        inputStream = urlConnection.getInputStream();
                    } else {
                        inputStream = urlConnection.getErrorStream();
                    }
                    // parse stream
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String temp, response = "";
                    while ((temp = bufferedReader.readLine()) != null) {
                        response += temp;
                    }
                    // put into JSONObject
                    jsonObject.put("Content", response);
                    jsonObject.put("Message", urlConnection.getResponseMessage());
                    jsonObject.put("Length", urlConnection.getContentLength());
                    jsonObject.put("Type", urlConnection.getContentType());
    
                    return jsonObject.toString();
                } catch (IOException | JSONException e) {
                    return e.toString();
                }
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Log.i(LOG_TAG, "POST\n" + result);
            }
        }
    

    UPDATE:

    With your PHP code, you can try the following update:

    ...
                    JSONObject json = new JSONObject();
                    json.put("key1", "sample value...");
                    requestBody = json.toString();
                    URL url = new URL(address);
                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestProperty("Content-Type", "application/json");
                    OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                    writer.write(requestBody);
                    writer.flush();
                    writer.close();
                    outputStream.close();
    ...
    

    You can read more at Deprecated HTTP Classes.

    Other good solutions for your reference:

    • OkHttp
    • Volley
    • Retrofit

    Hope this helps!

    0 讨论(0)
提交回复
热议问题