我已经搜索了一段时间但尚未找到解决方案。我发送一个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;
}
}
}
答案 0 :(得分:0)
如评论中所述,帖子正文应编码为UTF_8。