dongquexi1990 2015-04-02 17:34
浏览 36

too long

I have created an android mobile app on Eclipse Juno and I am trying to enter information from my mobile app to my database which is on php my Admin. The database is connected through a localhost wamp server off bitnami.

When I enter the data on the app it is saying that it has been entered successfully, however when I go to the database there is no data being stored. I was wondering if anyone could point me in what I am doing wrong.

My code in my app is:

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 com.example.independentretailers.R;
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class customersignup extends Activity {

EditText etFirstName, etSurname, etPhoneNumber, etEmail, etPassword;
Button bSave;
@SuppressLint("NewApi")
@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //setup strict mode policy 
    StrictMode.ThreadPolicy policy = new    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.customersignup);
    //for firstname
    etFirstName = (EditText) findViewById(R.id.editText1);
    //for surname 
    etSurname = (EditText) findViewById(R.id.editText2);
    //for phone number
    etPhoneNumber = (EditText) findViewById(R.id.editText3);
    //for email 
    etEmail = (EditText) findViewById(R.id.editText4); 
    //for password
    etPassword = (EditText) findViewById(R.id.editText5);
    //setting up ID for the button 
    bSave = (Button) findViewById(R.id.button1);
    //setting up onclick listener 
    bSave.setOnClickListener(new View.OnClickListener(){

        InputStream is = null;
        @Override 
        public void onClick(View arg0){
            //storing values inside edit texts inside strings 
            String FirstName = ""+etFirstName.getText().toString();
            String Surname = ""+etSurname.getText().toString(); 
            String PhoneNumber = ""+etPhoneNumber.getText().toString();
            String Email = ""+etEmail.getText().toString();
            String Password = ""+etPassword.getText().toString(); 

            //setting name pair values 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            //adding string values inside the name value pairs 
            nameValuePairs.add(new BasicNameValuePair("First Name",   FirstName));
            nameValuePairs.add(new BasicNameValuePair("Surname", Surname));
            nameValuePairs.add(new BasicNameValuePair("Phone Number",   PhoneNumber));
            nameValuePairs.add(new BasicNameValuePair("Email", Email));
            nameValuePairs.add(new BasicNameValuePair("Password",    Password));

            //setting up the connection inside the try catch 

            try{ 
                //setting up the default client

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new    HttpPost("http://10.0.2.2/tutorial.php");
            //passing the name value pair inside the httppost 

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpClient.execute(httpPost);
        //setting up the entity 
        HttpEntity entity = response.getEntity();

        //define input stream reader 
        is = entity.getContent(); 

        //displaying message for data entered successfully 
        String msg = "Data entered successfully";
        Toast.makeText(getApplicationContext(), msg,     Toast.LENGTH_LONG).show();
            }
            catch (ClientProtocolException e)
            {
                Log.e("ClientProtocol", "Log_tag");
                e.printStackTrace(); 
            } catch (IOException e) 
            { 
                Log.e("Log_tag", "IOException");
                e.printStackTrace();
            }

        }

    });


}
}

My PHP text file is:

$con=mysql_connect('localhost', 'root', 'pancakes');
mysql_select_db("independentretailers",$con); 

$FirstName=$_POST['First Name']; 
$Surname=$_POST['Surname'];
$PhoneNumber=$_POST['Phone Number'];
$Email=$_POST['Email'];
$Password=$_POST['Password'];

 mysql_query("insert into customer(First Name, Surname, Phone Number, Email,   Password)    values('{$FirstName}','{$Surname}','{$PhoneNumber}','{$Email}','{$Password}')");

//mysql_close();
?> 
  • 写回答

1条回答 默认 最新

  • douwei1944 2015-04-08 17:32
    关注

    you are printing the response at all. you are just toasting the msg which you have created. Update: You are running network related operation on the ui thread. You will get NetworkOnMainThreadException post honeycomb.

    Use a Thread or Asynctask.

    Check the answer to this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B