我搜索了一段时间,但尚未找到解决方案。我正在将httppost发送到我的php文件,然后将其存储在我的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/