简体   繁体   English

如何从Java程序获取摘要值到php脚本?

[英]How to get summary value from java program to php script?

I get string Operacinės sitemos from mysql using php script. 我使用php脚本从mysql获取字符串Operacin的sitemos But when I try to send this value again to php script and get other values I am getting null. 但是,当我尝试再次将此值发送到php脚本并获取其他值时,我将得到null。 I think problem might be with php script which gets non-english characters. 我认为问题可能出在非英语字符的php脚本上。 Am I missing something? 我想念什么吗?

Java code: 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 script: 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 爱迪

This php code works fine. 这个PHP代码工作正常。 I can see the needed output. 我可以看到所需的输出。 But why the script above doesn't work? 但是,为什么上面的脚本不起作用?

<?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();
?>

How to send utf-8_lithuanian_ci to php script? 如何发送utf-8_lithuanian_ci到PHP脚本?

That's very simple. 那很简单。 utf-8_lithuanian_ci is a collation, so the moment you have data with an order of that collation, it's already done - you only need to pass that data. utf-8_lithuanian_ci是一种排序规则,因此,当您按排序规则的顺序获取数据时,就已经完成了-您只需要传递该数据即可。 Collation can only be applied when sorting, so as the data is already sorted when you pass it, there is nothing else you need to do. 排序规则只能在排序时应用,因为传递数据时已经对数据进行了排序,所以您无需执行其他任何操作。

If you ask about the encoding, it can not be strictly said. 如果您询问编码,则不能严格说出来。 It's common you use the UTF-8 encoding here (as the collation is UTF-8 based), however, technically you can use any different character-encoding as well and as you have not specified which one, it remains imprecise. 通常在这里使用UTF-8编码(归类是基于UTF-8的),但是,从技术上讲,您也可以使用任何其他字符编码,并且由于您未指定哪一种,因此仍然不准确。

Additionally, the usage of SET NAMES utf8 within PHP is not recommended and the usage of the whole mysql extension is discouraged. 此外,对使用SET NAMES utf8 ,不推荐在PHP 整个MySQL扩展的使用是气馁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM