代码不通过Volley将数据发送到mysql

时间:2016-08-11 09:18:51

标签: java php android mysql android-volley

我正在尝试使用volley post方法更新mysql数据库,即使已成功调用php但数据库未获取已发送的值。我的php更新表,其中包含已发送的值和由php生成的唯一ID,php成功触发查询并更新id列,但不是从android发送的数据列。

以下是java代码

package com.attosectechnolabs.cardviewone;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class forum extends AppCompatActivity {

    String thread_text, User = "Test";
    Context ctx;
    EditText new_thread_text;
    TextView resulttemp;
    Button submit_thread, cancel_thread;
    private static final String REGISTER_URL = "http://attosectechnolabs.com/Projects/eduapp/thread.php";
    public static final String KEY_THREAD = "thread";
    public static final String KEY_USERNAME = "User";

    public forum() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        setContentView(R.layout.forum);

        submit_thread = (Button) findViewById(R.id.submit_thread);
        cancel_thread = (Button) findViewById(R.id.cancel_thread);
        new_thread_text = (EditText) findViewById(R.id.new_thread_text);
        resulttemp = (TextView) findViewById(R.id.resulttemp);

        submit_thread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                insertThreadText();

            }
        });
    }

    private void insertThreadText() {

        final String thread_text = new_thread_text.getText().toString().trim();
        resulttemp.setText(thread_text);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(forum.this, response, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(forum.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_THREAD, thread_text);
                params.put(KEY_USERNAME, User);
                return params;
            }

        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}
似乎我搞砸了帖子请求,但我不知道它是什么。

1 个答案:

答案 0 :(得分:1)

对不起,伙计们这是一个愚蠢的错误, 而不是

public static final String KEY_THREAD = "thread";

应该是

public static final String KEY_THREAD = "thread_text";
谢谢你的时间。