2012-01-22 13 views
8

to jest mój przykładowy kod, jednak użyłem HttpProtocolParams.setContentCharset (params, "utf-8"); im mój kod, ale kiedy wysyłam dane utf-8, otrzymuję tylko "?????" po stronie serwera (kod PHP)! Jaki jest problem?Wysyłanie zawartości utf8 z metodą postu na serwer w Androidzie przy użyciu HttpClient

HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, "utf-8"); 

    HttpClient httpclient = new DefaultHttpClient(params); 

    httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
    HttpPost httppost = new HttpPost("URL_TO_SERVER"); 

    try { 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("uname", name.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("uemail", email.getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("udesc", body.getText().toString())); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     HttpResponse hresponse = httpclient.execute(httppost); 

     String response = new Scanner(hresponse.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next(); 

     hresponse.getEntity().consumeContent(); 
     httpclient.getConnectionManager().shutdown(); 

     Log.d("WX", response.toString()); 
     Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

Odpowiedz

23

Może należy użyć UTF-8 dla podmiotu:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); 
+1

dzięki, że wystarczyły :) –

3

Pracowałem

public static String sendData(String data) { 
    String str = null; 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(serverurl); 
    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("s", data)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = client.execute(httppost); 
     str = new Scanner(response.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next(); 

     Log.v("srv response:", str); 
    } catch (SecurityException e) { 
     str = "err2"; 
    } catch (IOException error) { 
     str = "err1.1"; 
    } catch (NullPointerException e) { 
     str = "err1.2"; 
    } 
    return str; 
} 
Powiązane problemy