我是java和android的全新...我确定我在代码上做错了,请帮我修复它
我尝试将推文的内容作为字符串从Android应用程序发送到此在线解析器:http://demo.ark.cs.cmu.edu/parse,我想检索解析的结果,这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String Tobetag = urlEncode(contentTweet);
new Analyser().execute();
}
private String urlEncode(String s){
try {
return URLEncoder.encode(s,"UTF-8").replace("+", "%20");
}
catch ( UnsupportedEncodingException e) {
throw new RuntimeException(s,e);
}
}
private class Analyser extends AsyncTask<Integer, Integer, Integer>
{
private String mUrl = "http://demo.ark.cs.cmu.edu/parse?sentence=";
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(mUrl + mTobetag));
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader( new InputStreamReader(in) );
String result = reader.readLine();
System.out.println("Top");
System.out.println(result);
System.out.println("Bottom");
in.close();
}
catch(Exception e)
{
Log.e("Twitter", "doInBackground_" + e.toString());
}
return new Integer(1);
}
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(Integer i) {
}
}
我运行了代码,没有报告错误,但我没有得到解析结果,我只有这个:
我能做些什么来获得我想要的结果?
P.S。另外我不确定我是否正确使用了urlencoder,它将推文内容的斜线转换为%2F,但是当我通过浏览器使用在线解析器时,它表明它根本没有改变斜线,请帮助
答案 0 :(得分:2)
您只阅读回复的第一行,如果您想阅读整个回复,可以这样做:
String result = "";
for(String line;(line=br.readLine())!=null;){
result += line;
}
这将为您提供整个http://demo.ark.cs.cmu.edu/parse
页面作为回复,如果您希望响应只是一个JSON对象,请使用此网址http://demo.ark.cs.cmu.edu/parse/api/v1/parse?sentence=....