Java 从Maluuba API获取JSON值
我试图从android中与Maluuba API相关的url获取json值,浏览器中的json答案是:Java 从Maluuba API获取JSON值,java,android,json,Java,Android,Json,我试图从android中与Maluuba API相关的url获取json值,浏览器中的json答案是: { "entities":{ "message":["attend the meeting"], "dateRange":[{"start":"2013-12-28","end":"2013-12-29"}], "time":["03:00:00PM"]}, "action":"REMINDER_SET", "category":"REMINDER" } 我在android中的代码是: t
{
"entities":{
"message":["attend the meeting"],
"dateRange":[{"start":"2013-12-28","end":"2013-12-29"}],
"time":["03:00:00PM"]},
"action":"REMINDER_SET",
"category":"REMINDER"
}
我在android中的代码是:
try
{
String startd, endd, title;
httpclient = new DefaultHttpClient("napi.maluuba.com/v0/interpret?phrase='"+textdiary+"'&apikey=myapikey");
httppost = new HttpPost();
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("message",title));
nameValuePairs.add(new BasicNameValuePair("start",startd));
nameValuePairs.add(new BasicNameValuePair("end",endd));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
if(entity !=null)
{
InputStream instream=entity.getContent();
try {
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
JSONObject ja = jsonResponse.getJSONObject("entities");
String thetitle=ja.getString("message");
String thestarddate=ja.getString("start");
String theenddate=ja.getString("end");
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
}
}
}
catch(Exception ex)
{
Toast.makeText(getBaseContext(), ex.toString(),Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
试试看
{
字符串开始、结束、标题;
httpclient=newdefaulthttpclient(“napi.maluuba.com/v0/exploration?phrase='”+textdiary+“&apikey=myapikey”);
httppost=新的httppost();
试一试{
nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“消息”,标题));
添加(新的BasicNameValuePair(“开始”,startd));
添加(新的BasicNameValuePair(“end”,endd));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
如果(实体!=null)
{
InputStream instream=entity.getContent();
试一试{
JSONObject jsonResponse=新的JSONObject(convertStreamToString(instream));
JSONObject ja=jsonResponse.getJSONObject(“实体”);
String thetitle=ja.getString(“消息”);
String thestarddate=ja.getString(“开始”);
String theenddate=ja.getString(“结束”);
}
捕获(例外e)
{
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
}
}
捕获(例外情况除外)
{
Toast.makeText(getBaseContext(),例如toString(),Toast.LENGTH_LONG).show();
}
}捕获(例外e)
{
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
关键是我想得到“message”、“start”和“end”的值,显然我的代码不起作用,有人能帮我吗?“我没有得到我想要的值。”。好吧,那太糟糕了。那些标记是嵌套的JSON数组,你需要对它们进行两次解析才能得到值。你应该先用谷歌搜索一下