2013-07-20 2 views
0

Я не уверен, что проблема связана с PHP или Android-приложением, но я отправляю HttpPost с данными и получаю только ??? вставлен в мою таблицу базы данных. Я сделал следующее в PHP:приложение для Android не работает с utf8

 $db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect'); 
    mysql_query("SET NAMES utf8;"); 
    mysql_query("SET CHARACTER_SET utf8;"); 

затем в андроида я сделал следующее:

  String postReceiverUrl = "http://hwy.com/displaypost.php"; 
     HttpParams params = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(params, "utf-8"); 
     // HttpClient 
     HttpClient httpClient = new DefaultHttpClient(params); 

     // post header 
     HttpPost httpPost = new HttpPost(postReceiverUrl); 

     // add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 

     //All user input 
     EditText shopName = (EditText)findViewById(R.id.shopName); 
     EditText shopLocation = (EditText)findViewById(R.id.shopLocation); 
     EditText shopCity = (EditText)findViewById(R.id.shopCity); 
     EditText discountRate = (EditText)findViewById(R.id.shopDiscount); 
     EditText discountDuration = (EditText)findViewById(R.id.shopDiscountDuration); 

     nameValuePairs.add(new BasicNameValuePair("name", shopName.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("location", shopLocation.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("city", shopCity.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("rate", discountRate.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("duration", discountDuration.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("category", shopCategory)); 

     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // execute HTTP post request 

     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v("", "Response: " + responseStr); 

      // you can add an if statement here and do other actions based on the response 
     } 

ответ я получаю в LogCat является:

07-20 10:52:56.996: V/(7749): Response: Array 
07-20 10:52:56.996: V/(7749): (
07-20 10:52:56.996: V/(7749):  [name] => ??????? 
07-20 10:52:56.996: V/(7749):  [location] => ??????? 
07-20 10:52:56.996: V/(7749):  [city] => ??????? 
07-20 10:52:56.996: V/(7749):  [rate] => ??????? 
07-20 10:52:56.996: V/(7749):  [duration] => ?????? 
07-20 10:52:56.996: V/(7749):  [category] => ????? ? ????????? 
07-20 10:52:56.996: V/(7749):) 

Любая идея, почему это ломать?

заранее спасибо

ответ

0

Ответ был найден после долгих исследований и экспериментов. Это он:

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 

Далее следуют:

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); 

Это все есть на него! Надеюсь, кому-то это поможет в один прекрасный день

Смежные вопросы