BhargavSushant BhargavSushant - 3 months ago 30
Android Question

Code not sending data over Volley to mysql

I am trying to update mysql database using volley post method, even though php is successfully being called yet database is not getting sent values. My php updates table with sent values and a unique id that is genereated by php , php successfully fires query and updates the id column but not the data column that was sent from android.

Following is the java code

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);
}
}


it seems I am messing up the post request but I don't know what it is.

Answer

Sorry guys it was a silly mistake, instead of

public static final String KEY_THREAD = "thread";

it should have been

public static final String KEY_THREAD = "thread_text";

thank you for your time.