【问题标题】:Migrating POST method to Volley将 POST 方法迁移到 Volley
【发布时间】:2016-04-11 01:25:10
【问题描述】:

我已经和应用程序连接到具有基本功能(获取、插入、更新、延迟)的在线数据库我使用 JSON 和“http 方式”完成了它,一切正常,现在我想将我的代码迁移到 volley但我对 post 方法有一些问题。

这是我的旧代码,它可以作为新的 get 方法正常工作

case "3"://insert
try {
                    HttpURLConnection urlConn;
                    DataOutputStream printout;
                    DataInputStream input;
                    miurl = new URL(cadena);
                    urlConn = (HttpURLConnection) miurl.openConnection();
                    urlConn.setDoInput(true);
                    urlConn.setDoOutput(true);
                    urlConn.setUseCaches(false);
                    urlConn.setRequestProperty("Content-Type", "application/json");
                    urlConn.setRequestProperty("Accept", "application/json");
                    urlConn.connect();

                    JSONObject jsonParam = new JSONObject();
                    jsonParam.put("name", params[2]);
                    jsonParam.put("email",params[3]);

                    OutputStream os = urlConn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
                    writer.write(jsonParam.toString());
                    writer.flush();
                    writer.close();

                    int respuesta = urlConn.getResponseCode();


                    StringBuilder result = new StringBuilder();

                    if (respuesta == HttpURLConnection.HTTP_OK) {

                        String line;
                        BufferedReader br=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                        while ((line=br.readLine()) != null) {
                            result.append(line);
                        }

                        JSONObject respuestaJSON = new JSONObject(result.toString());  

                        String resultJSON = respuestaJSON.getString("status");

                        if (resultJSON == "1") {     
                            answer = "Ok!";

                        } else if (resultJSON == "2") {
                            answer = "Error";
                        }

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return answer;

                ...

    @Override
    protected void onPostExecute(String s) {
         tv_answer.setText(s);
        //super.onPostExecute(s);
    }

而新的尝试使用凌空抽射

case R.id.btn_insert:
            url = INSERT;
            StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response) {
                            tv_answer.setText(response);
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            tv_answer.setText("That didn't work!");
                        }
                    }
            ) {
                @Override
                protected Map<String, String> getParams()
                {
                    Map<String, String>  params = new HashMap<String, String>();
                    headers.put("name", et_name.getText().toString());
                    headers.put("email", et_email.getText().toString());

                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type", "application/json");
                    params.put("Accept", "application/json");

                    return params;
                }
            };
            queue.add(postRequest);
             break;

我刚刚收到一个 SQL 致命错误,说我的列不能为空,换句话说,我的函数正在发送两个空参数,我不是专家,我在教程的一些帮助下完成了第一部分,所以现在我有点迷茫

【问题讨论】:

    标签: android json http post android-volley


    【解决方案1】:

    请使用getBody(),而不是getParams()

    您可以在以下链接中参考我的一些答案:

    Volley send JSONObject to server with POST method

    Sending a POST request with JSONArray using Volley

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      只需使用 JSONObjectRequest 而不是 StringRequest,因为您在上一个示例中发送 json

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-13
        • 2015-11-18
        • 1970-01-01
        • 2014-11-12
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多