0

I am new to android development. Am making the app where in the user will get registered with us through the registration form in android. But the i don't get any errors nor i get the output.

Here's the source code for java:

package com.example.entrepreneurexpress;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InvestorRegister extends Activity {

    EditText YourName;
    EditText email;
    EditText password;
    EditText confirmPassword;
    Button btnClear, btnRegister;

    String nm, emailAdd, cnfPass, pwd;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.investors_registration);
        ActionBar aBar = getActionBar();
        aBar.setDisplayHomeAsUpEnabled(true);

        YourName = (EditText) findViewById(R.id.invRegName);
        email = (EditText) findViewById(R.id.invRegEmail);
        password = (EditText) findViewById(R.id.invRegPassword);
        confirmPassword = (EditText) findViewById(R.id.invRegConfPassword);

        btnClear = (Button) findViewById(R.id.btnInvRegClear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (YourName.length() >= 1) {
                    YourName.setText("");
                }
                if (email.length() >= 1) {
                    email.setText("");
                }
                if (password.length() >= 1) {
                    password.setText("");
                }
                if (confirmPassword.length() >= 1) {
                    confirmPassword.setText("");
                }
            }
        });

        btnRegister = (Button) findViewById(R.id.btnInvRegRegister);
        btnRegister.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(
                    (!YourName.getText().toString().equals("")) &&
                    (!email.getText().toString().equals("")) &&
                    (!password.getText().toString().equals(""))
                  ) {
                    /*Toast.makeText(getApplicationContext(), "Registered !", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(getApplicationContext(), WelcomeInvestor.class);
                    startActivity(i);*/
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                HttpClient httpclient = new DefaultHttpClient();
                                HttpPost httppost = new HttpPost("http://122.170.107.27/AdroidApp/insert.php");

                                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                                nameValuePairs.add(new BasicNameValuePair("invName", YourName.getText().toString()));
                                nameValuePairs.add(new BasicNameValuePair("invPassword", password.getText().toString()));
                                nameValuePairs.add(new BasicNameValuePair("invEmail", email.getText().toString()));

                                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                HttpResponse response = httpclient.execute(httppost);
                                InputStream inputStream = response.getEntity().getContent();

                                int status = response.getStatusLine().getStatusCode();

                                if(status == 200) {
                                    Toast.makeText(getApplicationContext(), "Registered !", Toast.LENGTH_SHORT).show();
                                    Intent i = new Intent(getApplicationContext(), WelcomeInvestor.class);
                                    startActivity(i);
                                } else {
                                    Toast.makeText(getApplicationContext(), "Some Error Occured !", Toast.LENGTH_SHORT).show();
                                }
                            } catch(Exception e) {
                                Toast.makeText(getApplicationContext(), "Error "+e.toString(), Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    thread.start();
                } else {
                    Toast.makeText(getApplicationContext(), "Please Fill In All The Details!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

And here's the php code:

<?php

$output = array();

    $con = mysqli_connect("localhost", "root", "root", "AndroidApp");
    if(!$con) {
        echo "Connection Aborted !<br />" . mysqli_error($con);
    } else {
        echo "Connection Successful !<br />";
    }

    $name = isset($_POST['fullname']) ? $_POST['fullname'] : '';
    $email = isset($_POST['emailAddress']) ? $_POST['emailAddress'] : '';
    $pasword = isset($_POST['passWord']) ? $_POST['passWord'] : '';

    $query = mysqli_query($con, "INSERT INTO Investors
                                    (`invName`, `invEmail`, `invPassword`)
                                VALUES('".$name."', '".$email."', '".$pasword."')");

    if ($query)
    {
        $output["success"] = 1;
        $output["message"] = "Successfully Inserted";

        echo jsonencode($output);
    } else {
        $output["success"] = 0;
        $output["message"] = "insertion failed......";

        echo json_encode($output);  
    }

    mysqli_close($con);
?>

Am getting this error now after updating java code:

04-10 14:02:26.403: E/linker(29292): load_library(linker.cpp:759): library "libmaliinstr.so" not found

I have tried my best, but failed. Kindly help me out. Thanks.

Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
  • Typo? `echo jsonencode($output);` shouldn't that be `echo json_encode($output);` – Funk Forty Niner Apr 09 '14 at 07:57
  • `null` values are inserted... – Saiyan Prince Apr 09 '14 at 07:59
  • You didn't answer my question. – Funk Forty Niner Apr 09 '14 at 08:01
  • Corrected.. getting null values, i mean i can't see anything in the database accept the id part as it is auto_increment – Saiyan Prince Apr 09 '14 at 08:04
  • All I can see are these lines `String nm, emailAdd, cnfPass, pwd;` and `insertRecords(nm, pwd, emailAdd);` and `(String fullname, String passWord, String emailAddress)` which seem inconsistent. Then again, I know nothing about Android, but I do know SQL and pattern matching, so that's all I can make out of this. Asides from making that sure your table's column names are correct. – Funk Forty Niner Apr 09 '14 at 08:09

1 Answers1

0

First of all, Strict mode is only for development purpose, to debug. And not to be used while publishing. All network communications must be made outside the main thread. If you use Strict mode - permitAll(), you are messing with the normal operation. So use AsynchTask for whatever network communications you want.

You can find the tutorial here.

http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask

ALso, here you are directly trying to get the response as soon as you execute httpclient.execute(httppost); I don't think this is going to work, as the server needs time to respond.

In AsynchTask, you have a method onPostExecute which will handle this properly.

Read this http://blog.vogella.com/2012/02/22/android-strictmode-networkonmainthreadexception/ http://blog.vogella.com/2011/04/04/android-strictmode/

Xpleria
  • 5,472
  • 5
  • 52
  • 66
  • I know that Strict mode is only for development purpose and not to be used while publishing. Kindly give me the code of `Asynch` class and please tell me where should i put it in my code ? – Saiyan Prince Apr 09 '14 at 08:42
  • I have given the link for the for AsynchTask tutorial in my answer above. You need to create a class and extend AsynchTask. It can be a nested class or a separate class. Follow the tutorial. It may be a little difficult to understand in the beginning. Read thouroughly before you try implementing. – Xpleria Apr 09 '14 at 08:47
  • see this http://stackoverflow.com/questions/20494899/how-to-solve-this-java-nio-bufferoverflowexception-error and DO NOT use suppress lint instead correct your code..unless its impossible to have a work around without it..suppress lint hides all your errors/flaws while coding.. – Xpleria Apr 09 '14 at 16:05