如何从java程序到PHP脚本获取汇总值?

时间:2012-04-29 08:12:48

标签: java php mysql encoding

我使用php脚本从mysql获取字符串Operacinėssitemos。但是当我尝试将此值再次发送到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();
    ?>

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 个答案:

答案 0 :(得分:0)

  

如何将utf-8_lithuanian_ci发送到php脚本?

这非常简单。 utf-8_lithuanian_ci是一个排序规则,因此,当您拥有该排序顺序的数据时,它已经完成 - 您只需要传递该数据。排序只能在排序时应用,因为当您传递数据时数据已经排序,您无需做任何其他事情。

如果您询问编码,则无法严格说明。在这里使用UTF-8编码很常见(因为整理是基于UTF-8的),但是,从技术上讲,你也可以使用任何不同的字符编码,而你没有指定哪一个,它仍然不精确

此外,不建议在PHP中使用SET NAMES utf8 不鼓励使用整个mysql扩展名。