使用Volley的HTTP Post请求

时间:2016-05-03 17:30:42

标签: android android-volley

我编码使用Volley使用HTTP post发送一些消息和电子邮件。 当我使用Genymotion.Everything luking运行模拟器,但我点击单击按钮,它显示HTTP已被停止。我正在给logcat图片Please click this Logcat picture to see请帮我解决错误以及如何解决这个问题

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String URL_CLICK = "http://www.careoservice.goyalsoftwares.com/feedback/add.php";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EMAIL = "email";

    private EditText editTextMessages;
    private EditText editTextEmail;

    private Button buttonSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextEmail = (EditText) findViewById(R.id.editmsg);
        editTextEmail = (EditText) findViewById(R.id.editmail);

        buttonSend = (Button) findViewById(R.id.btnclk);

        buttonSend.setOnClickListener(this);
    }

    public void sendView() throws JSONException {
        final String message = editTextMessages.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CLICK, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Toast.makeText(MainActivity.this,s, Toast.LENGTH_LONG).show();
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_MESSAGE, message);
                params.put(KEY_EMAIL, email);
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        if (v == buttonSend) {
            try {
                sendView();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您没有初始化“editTextMessages”,这是您的空指针异常。