Response:wrong. [user Registration Via Android App Using Http Post Not Working]
Solution 1:
try below code:-
HttpResponsehttpResponse= httpClient.execute(httpPost);
if (httpResponse != null)
{
InputStreamin= httpResponse.getEntity().getContent();
result = ConvertStreamToString.convertStreamToString(in);
// System.out.println("result =>" + result);
}
convertStreamToString method
public String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
do not under stand :-
HttpResponsehttpResponse= httpClient.execute(httpPost); // getting response from server // where you use this response and why using below line or whats the benefit of below line
ResponseHandler<String> responseHandler = newBasicResponseHandler();
finalStringresponse= httpClient.execute(httpPost, responseHandler);
Solution 2:
Follow that tutorial very usefull, and clear how to work on json webservices with android.
Solution 3:
Try this code using AsyncTask
publicclassRegisterextendsActivityimplementsOnClickListener{
privateStringmTitle="Write.My.Action";
privatestaticfinalStringLOGTAG="tag";
public EditText fullname, email, password;
private Button register;
private ProgressDialog mDialog;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
getActionBar().setTitle(mTitle);
fullname = (EditText) findViewById(R.id.fullname);
email = (EditText) findViewById(R.id.editText2);
password = (EditText) findViewById(R.id.editText1);
register = (Button) findViewById(R.id.button1);
register.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v) {
switch (v.getId()) {
case R.id.button1:
mDialog = newProgressDialog(Register.this);
mDialog.setMessage("Attempting to Register...");
mDialog.setIndeterminate(false);
mDialog.setCancelable(false);
mDialog.show();
newRegisterUser().execute();
}
}
publicclassRegisterUserextendsAsyncTask<Void, Void, String>
{
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
}
@Overrideprotected String doInBackground(Void... params) {
// TODO Auto-generated method stubHttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost("myurl");
System.out.println("httpPost is: " + httpPost);
Stringfullname_input= fullname.getText().toString().trim();
Stringemail_input= email.getText().toString().trim();
Stringpassword_input= password.getText().toString().trim();
//adding data into list view so we can make post over the server
List<NameValuePair> nameValuePair = newArrayList<NameValuePair>();
nameValuePair.add(newBasicNameValuePair("fullname", fullname_input));
nameValuePair.add(newBasicNameValuePair("email", email_input));
nameValuePair.add(newBasicNameValuePair("password", password_input));
System.out.println("namevaluepair is: " + nameValuePair);
httpPost.setEntity(newUrlEncodedFormEntity(nameValuePair));
//execute http post resquestHttpResponsehttpResponse= httpClient.execute(httpPost);
ResponseHandler<String> responseHandler = newBasicResponseHandler();
finalStringresponse= httpClient.execute(httpPost, responseHandler);
System.out.println("Response is: " + response);
return response;
}
@OverrideprotectedvoidonPostExecute(String result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
if(response.equalsIgnoreCase("Signed Up")){
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
startActivity(newIntent(Register.this, Registration_Success.class));
}
});
}else {
showAlert();
}
}
}
publicvoidshowAlert(){
Register.this.runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
AlertDialog.Builderbuilder=newAlertDialog.Builder(Register.this);
builder.setTitle("Registration Error");
builder.setMessage("Please, try registration again!")
.setCancelable(false)
.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialogalert= builder.create();
alert.show();
}
});
}
}
Hope this helps you!!!
If its not working please let me know i will try to help more...
Solution 4:
Try using Volley if not. It's faster than the ussual HTTP connection classes. Example:
RequestQueue queue = Volley.newRequestQueue(this);
String url = "url";
JSONObject juser = newJSONObject();
try {
juser.put("locationId", locID);
juser.put("email", email_input);
juser.put("password", password_input);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonObjectRequest jsObjRequest = newJsonObjectRequest(Request.Method.POST, url, juser, newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONObject response) {
// TODO Auto-generated method stub
txtDisplay.setText("Response => "+response.toString());
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
Solution 5:
Like @Andrew T suggested , I am posting my solution.
The mysql_error()
helped me debug the issue. mysql_error() displayed in the Logcat that "register table is not found" .. but the table name is registers. I was missing "s" at the end.
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$insert_data = "INSERT INTO registers(id, fullname, email, password)
Values ('','$fullname', '$email', '$password')";
$insert_result = mysql_query($insert_data) or die(mysql_error());
//echo "Signed Up";
if($insert_result){
echo"Signed Up";
}
else{
echo"wrong!";
}
Everything worked perfect after that. Again, thank you all for your time and solutions. Everyone's suggestions are great, but I can not accept any answer at this point.. sorry:(
Post a Comment for "Response:wrong. [user Registration Via Android App Using Http Post Not Working]"