php - httppost mysql 为空

标签 php android mysql http-post

我已经搜索了一段时间,但还没有找到解决方案。我正在向我的 php 文件发送一个 httppost,然后将其存储在我的 mysql 数据库中。但是当我检查数据库时,我看到空帖子。我看到它插入了条目,但值是空的。我已经检查了我的 php 代码,如果我使用信息手动 ping url,它会将其放入数据库中。我的 logcat 显示没有错误。

public class Add extends Activity implements View.OnClickListener
{
/** Called when the activity is first created. */
private Button add_btn;
EditText name;
EditText addy;
EditText hours;
EditText desc;
TextView error;
List<NameValuePair> nameValuePairs;
InputStream is;

  @Override
public void onCreate(Bundle savedInstanceState) 
{
      super.onCreate(savedInstanceState);
      setContentView(R.layout.add);

      add_btn = (Button)findViewById(R.id.add_btn);
      add_btn.setOnClickListener((OnClickListener) this);

      name = (EditText)findViewById(R.id.action_name);
      addy = (EditText)findViewById(R.id.action_addy);
      hours = (EditText)findViewById(R.id.action_hours);
      desc = (EditText)findViewById(R.id.action_desc);
      error = (TextView)findViewById(R.id.error_text);
}

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

public void onClick(View v)
{ 
    if(v == add_btn)
    {
        if(name.getText().length()== 0)
        {
            error.setText("You Must enter a bussiness name.");
        }
        else if(desc.getText().length()== 0)
        {
            error.setText("Please enter a short description. I.E. Beer Bar or Dance Club");
        }
        else
        {
            new Connect().execute();

            name.setText("");
            addy.setText("");
            hours.setText("");
            desc.setText("");
        } 
    }
}

private class Connect extends AsyncTask<Void, Void, Void> 
{

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();    
    }

    @Override
    protected Void doInBackground(Void... params) 
    {           
        try
        {
            String a = name.getText().toString().trim();
            String b = addy.getText().toString().trim();
            String c = hours.getText().toString().trim();
            String d = desc.getText().toString().trim();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new
                    HttpPost("http://www.website.com/phpfile.php");

            nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("bname", a));
            nameValuePairs.add(new BasicNameValuePair("baddy", b));
            nameValuePairs.add(new BasicNameValuePair("bhours", c));
            nameValuePairs.add(new BasicNameValuePair("bdesc", d));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_16));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("Server Response: ", " " + response);
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection "+e.toString());
            //error.setText("Connection error");
        }
        return null;
    }
}
}

最佳答案

正如评论中所发布的,帖子正文应编码为 UTF_8。

关于php - httppost mysql 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355997/

相关文章:

java - 我正在使用 Live sdk 将文件上传到 One Drive,并收到来自 google 的警报 :

PHP MySQL : Display all column names from a given table and the values for any single given row

mysql - API 服务器返回 404 错误

javascript - YouTube 嵌入视频参数不起作用

php - 管理登录用户

android - 列表背景的自定义选择器

java - 在Android项目中包含另一个项目

mysql - 使用逗号转义将 SQL 表导出到 CSV?

PHP MySQL根据变量更改选择查询

php - 执行必要步骤后未配置 Lumen 数据库 []