我使用 php 脚本从 mysql获取字符串Operacinės sitemos 。但是当我尝试再次将此值发送到 php 脚本并获取其他值时,我得到了 null。我认为问题可能出在获取非英文字符的 php 脚本上。我错过了什么吗?
Java代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_course_layout);
Intent in = getIntent();
courseName = in.getStringExtra("fullname");
firstname = in.getStringExtra("firstname");
lastname = in.getStringExtra("lastname");
TextView label = (TextView)findViewById(R.id.label);
label.setText(courseName);
String summary = null;
TextView summaryContent = (TextView)findViewById(R.id.summary);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cname",""+courseName));
InputStream is = null;
String result = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("********");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"ISO-8859-10"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
System.out.println(line);
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int ii=0;ii<jArray.length();ii++){
JSONObject json_data = jArray.getJSONObject(ii);
summary = json_data.getString("summary");
}
summaryContent.setText(summary);
} catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
};
PHP 脚本:
<?php
mysql_connect("localhost","***","*****");
mysql_select_db("*******");
mysql_query("SET NAMES utf8");
$fullname = mysql_real_escape_string($_REQUEST['cname']);
$q=mysql_query("SELECT mdl_course.summary FROM mdl_course WHERE mdl_course.fullname = '$fullname'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
已编辑
这个 php 代码工作正常。我可以看到所需的输出。但是为什么上面的脚本不起作用?
<?php
mysql_connect("localhost","*******","*********");
mysql_select_db("************");
$fullname = 'Operacinės sistemos';
$q=mysql_query("SELECT mdl_course.summary FROM mdl_course WHERE mdl_course.fullname = '$fullname'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>