我想通过套接字将与ElasticSeach一起存储的UTF-8文本发送到应用程序。
我有一个ThreadedTCPServer已实现,这是应该处理回复的类。
我已经实现了基本的基于字符串的握手来分享一些信息,比如发送了查询,并且会发送回复。
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
es = Elasticsearch()
#receive query from the client
query = self.request.recv(1024)
#Cut off the characters that aren't recognized
query=query[2:]
#for testing
query=query.lower().strip().replace(' ','_')
print query
#Send response that query was received
self.request.send("200...OK\n")
res = es.search(index="painters",body={"query": { "match" : {"title" : query}},"size":1 })
if res['hits']['hits']:
response = res['hits']['hits'][0]['_source']['text']
self.request.send("201...RE\n")
print response
response=response.encode('utf-8')
self.request.sendall(response)
在android方面,我有两个函数用于读取响应,一个用于读取字节。
private String getResponse(InputStream is){
String line="";
BufferedReader rd = new BufferedReader(new InputStreamReader(is),8);
try{
line=rd.readLine();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "Stream Exception", Toast.LENGTH_SHORT).show();
}
return line;
}
private String convertStreamToString(InputStream is) {
BufferedInputStream bi = new BufferedInputStream(is);
byte[] b = new byte[1024];
StringBuilder total = new StringBuilder();
try {
while (bi.read(b,0,1024)!=-1)
{
total.append(decodeUTF8(b));
Log.d("TOTAL",decodeUTF8(b));
}
} catch (Exception e) {
e.printStackTrace();
}
return total.toString();
}
这是应解码字符串的函数:
String decodeUTF8(byte[] bytes) {
return new String(bytes, UTF8_CHARSET);
}
问题是有时不是整个字符串显示在android端, 当整个事情经历一些UTF-8字符最终变形(完全不同于发送的字符)
AsyncTask post执行启动新的Activty:
protected void onPostExecute(String s) {
//super.onPostExecute(s);
if (s.contains("ECONNREFUSED")){
Toast.makeText(MainActivity.this,"Connection Failed",Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(MainActivity.this,ReplyActivity.class);
intent.putExtra(EXTRA_MESSAGE,s);
startActivity(intent);
}
获取字符串的新意图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get message
Intent intent = getIntent();
String summary = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
示例输出:
早年(1928年至1949年)
安迪·沃霍尔(“né”Andrej Varhola,Jr。)于1928年8月6日出生于宾夕法尼亚州匹兹堡。他是Andrij Warhola的第四个孩子(美国化为Andrew Warhola,Sr。,1889 - 1942年)和Júlia(“née”ZavackÃ,1892年 - 1972年),w
正如你所看到的,即使从android发送查询到python,我也会得到一些我需要切断的垃圾。
这里:
#Cut off the characters that aren't recognized
query=query[2:]
再版(响应):
<h2>Early life (1928\xe2\x80\x931949)</h2>\nAndy Warhol ("n\xc3\xa9" Andrej Varhola, Jr.) was born on August 6, 1928 in <a href="Pittsburgh">Pittsburgh</a>, Pennsylvania. He was the fourth child of Andrij Warhola (Americanized as Andrew Warhola, Sr., 1889\xe2\x80\x931942) and <a href="Julia Warhola">J\xc3\xbalia</a> ("n\xc3\xa9e" Zavack\xc3\xa1, 1892\xe2\x80\x931972), whose first child was born in their homeland and died before their move to the U.S.
终端打印:
<h2>Early life (1928–1949)</h2>
Andy Warhol ("né" Andrej Varhola, Jr.) was born on August 6, 1928 in <a href="Pittsburgh">Pittsburgh</a>, Pennsylvania. He was the fourth child of Andrij Warhola (Americanized as Andrew Warhola, Sr., 1889–1942) and <a href="Julia Warhola">Júlia</a> ("née" Zavacká, 1892–1972), whose first child was born in their homeland and died before their move to the U.S.