HttpPost 不工作它没有显示任何错误,但它没有在 php localhost 中存储任何内容,

HttpPost not working it is showing no error but it is not storing anything in php localhost,

此代码在单击按钮时不起作用,它不显示 toast 消息,也不向本地主机 php 服务器发送数据。我正在运行一个 android 硬件设备进行调试,请让我知道代码哪里不正确。

public class MainActivity extends Activity {

EditText etName,etEmail,etAge;
Button bSubmit;
@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.activity_main);

    //For the name
    etName = (EditText) findViewById(R.id.editName);
    //For the Age
    etAge  = (EditText) findViewById(R.id.editAge);
    //For the Email
    etEmail = (EditText) findViewById(R.id.editEmail);

    //setup Onclick Button
    bSubmit = (Button) findViewById(R.id.buttonSubmit);

    bSubmit.setOnClickListener(new View.OnClickListener() {
        //Onclick Listener
        InputStream is = null;
        @Override
        public void onClick(View arg0) {

           //Storing Values inside the string
            String name = ""+etName.getText().toString();
            String age = ""+etAge.getText().toString();
            String email = ""+etEmail.getText().toString();

            //Setting name pair values
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


            //Adding String Variable inside the name value pairs
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("age", age));
            nameValuePairs.add(new BasicNameValuePair("email",email));
            //Setting up HTTP Client

            try{

                HttpClient httpClient =  new DefaultHttpClient();
                HttpPost httpPost= new HttpPost("http://10.0.2.2:1337/tutorial.php");
                //Passing the name value
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                //Getting the response
                HttpResponse response = httpClient.execute(httpPost);

                //Setting up the entity
                HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                        //Display if data entered successfully
                String msg = "Data entered successfully";
                Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
                //Setting up the content inside an input stream

            }catch (ClientProtocolException e){
               Log.e("ClientProtocol","Log_tag");
                e.printStackTrace();
            }catch (IOException e)
            {
                Log.e("Log tag","IOException");
                e.printStackTrace();

            }
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

(编辑) 这是我正在使用的 php 文件代码 Xampp (localhost:1337) $name=isset($_POST['name']; $age=isset($_POST['age']); $email=isset($_POST['email']);

   mysql_query("insert into newtable(name,age,email) values('{$name}','{$age}','{$email}')"); 

这里有几个错误,我一一列举。

  1. 根据您的描述,您提到您的 运行 在本地主机 PHP 服务器(我假设它是 WAMP 服务器),并且您还提到您是 运行 真实硬件设备中的应用程序。如果你的 运行 在真实设备中,那么 localhost 是没有用的。您需要自己的服务器(您可以找到很多免费托管网站)。如果您只想使用 WAMP,请在模拟器中对其进行测试。

  2. 不允许在主线程中执行网络操作(这就是您使用严格模式的原因 - 删除这两行)。学习使用 Async Task 进行网络操作。

这对你来说是一个很好的参考

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/