2012-04-29 41 views
0

我得到字符串從MySQL使用PHP腳本Operacinėssitemos。但是,當我嘗試再次將這個值發送到php腳本並獲得其他值時,我變得空。我認爲問題可能是使用非英文字符的PHP腳本。我錯過了什麼嗎?如何從java程序獲取摘要值到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(); 
    ?> 

Eddited

這個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(); 
?> 
+1

也許'mysql_set_charset(「utf8」);'會比'mysql_query(「SET NAMES utf8」);' – 2012-04-29 08:21:48

回答

0

如何發送UTF-8_lithuanian_ci到PHP腳本?

這很簡單。 utf-8_lithuanian_ci是排序規則,因此,當您具有排序規則的數據時,它已經完成 - 您只需要傳遞該數據。排序只能在排序時應用,因爲數據在您通過排序時已經排序,所以您無需執行其他任何操作。

如果您詢問編碼,不能嚴格地說。這裏通常使用UTF-8編碼(因爲排序規則是基於UTF-8的),但是,從技術上講,您可以使用任何不同的字符編碼,因爲您尚未指定哪一個,所以它仍然不準確。

此外,不建議在PHP中使用SET NAMES utf8不鼓勵使用整個mysql擴展。

+0

更好嗎? BufferedReader reader = new BufferedReader(new InputStreamReader(is,「iso-8859-10」),8); – LTnewbie 2012-04-29 08:25:18

+0

嘗試用UTF-8替換ISO -...。只是一個快速的猜測,如果這能解決你的問題,那就快速測試,否則你需要更精確地進行調試(嗯,做實際的調試,不要猜測,猜測對於兩分鐘的修復是可以的,但如果需要更長的時間,停止猜測)。 – hakre 2012-04-29 08:26:36

+0

不,它沒有幫助。我仍然無法獲得摘要值 – LTnewbie 2012-04-29 08:28:57

相關問題