本文介绍了将阿拉伯语从android发送到PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用下面的代码将阿拉伯文本从android发送到PHP文件
trying to send Arabic text from android to PHP file using the code below
Button.setOnClickListener(new OnClickListener() {
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
tv = (TextView)findViewById(R.id.from_t);
nv = (TextView)findViewById(R.id.to_t);
rd = (TextView)findViewById(R.id.agent_u);
try {
postData();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void postData() throws JSONException, UnsupportedEncodingException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
String url = "http://XXX.XXX.XXX.XXX/post.php";
HttpPost post = new HttpPost(url);
HttpParams params=post.getParams();
JSONObject json = new JSONObject();
params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7" );
try {
// JSON data:
json.put("name", tv.getText());
json.put("position",rd.getText());
json.put("position1",nv.getText());
JSONArray postjson=new JSONArray();
postjson.put(json);
post.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
post.setHeader("Accept-Charset", "UTF-8");
post.setHeader("json",json.toString());
//this is somthing els
System.out.print(json);
HttpResponse response = httpclient.execute(post);
Toast.makeText(getApplicationContext(), "SEND",Toast.LENGTH_SHORT).show();
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
}
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
return;
}
}
});
}
代码很好,但如果我发送阿拉伯文本,我会不断收到数字和拉丁字母.有什么帮助吗?谢谢
the code is fine but I keep getting numbers and and Latin letters if I send Arabic text.any help? thank you
推荐答案
如果你不想在 url 中发送 json 则使用这种方式.它可能会帮助你.
if you don't want to send json in url then use this way. it may help you.
InputStream is;
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
nameValuePairs1.add(new BasicNameValuePair("user_id", ""));
nameValuePairs1.add(new BasicNameValuePair("product_id", ""));
nameValuePairs1.add(new BasicNameValuePair("product_review",""+text));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity entity = responce.getEntity();
is = entity.getContent();
BufferedReader bufr = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(bufr.readLine() + "\n");
String line = "0";
while ((line = bufr.readLine()) != null)
{
sb.append(line + "\n");
}
is1.close();
result = sb.toString();
这篇关于将阿拉伯语从android发送到PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!