将POST方法迁移到Volley

时间:2016-04-11 01:25:10

标签: android json http post android-volley

我已经和应用程序连接到具有基本功能(获取,插入,更新,降级)的在线数据库,我使用JSON和" http方式"一切正常,现在我想将我的代码迁移到凌空,但我对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致命错误,告诉我列不能为空,换句话说我的函数发送两个空参数,我不是专家,我在教程中提供了一些帮助,因此我做了第一部分现在我有点迷失了

1 个答案:

答案 0 :(得分:0)

请使用getParams()

,而不是getBody()

您可以通过以下链接参考我的一些答案:

  

Volley send JSONObject to server with POST method

     

Sending a POST request with JSONArray using Volley

希望它有所帮助!