2014-01-25 59 views
0

我已經搜索了一段時間,但還沒有找到解決方案。我發送一個httppost到我的php文件,然後將它存儲在我的mysql數據庫中。但是,當我檢查數據庫時,我看到空的帖子。我看到它插入的條目,但值是空的。我檢查了我的php代碼,如果我手動ping它的信息將它放入數據庫的網址。我的logcat顯示沒有錯誤。Httppost mysql是空的

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

您的服務器期望郵編正文參數編碼爲UTF_16?通常情況下,我認爲它可能是UTF_8,也可能不是問題。編輯:認爲我已經發現了這個問題,檢查下面的發佈的答案。 – Ryan

+0

優秀..這是解決方案。非常感謝,我一直在尋找幾個小時。 – Jayce

回答

0

正如評論中發佈的,帖子正文應該被編碼爲UTF_8。

+0

瑞恩這是你的第一件事關於UTF 8編輯你的答案,我會接受它。再次感謝。 – Jayce

+0

太棒了,很高興它解決了。 – Ryan