Android 从url Alchemy获取json值
我试图从android中与Alchemy API相关的url中获取json值,浏览器中的json答案是:Android 从url Alchemy获取json值,android,json,alchemyapi,Android,Json,Alchemyapi,我试图从android中与Alchemy API相关的url中获取json值,浏览器中的json答案是: { "status": "OK", "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/compa
{
"status": "OK",
"usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
"url": "",
"language": "english",
"docSentiment": {
"type": "negative",
"score": "-0.423469"
}
}
我在android中的代码是:
package com.example.daymanger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
import android.widget.Toast;
public class Alchemy extends Activity {
StringBuilder total;
HttpClient httpclient;
HttpPost httppost;
JSONArray arr;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
String thelangage;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.alchemy);
TextView al=(TextView)findViewById(R.id.alchemy_text);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("myurl");
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("type",thelangage));
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));
JSONArray ja = jsonResponse.getJSONArray("docSentiment");
JSONObject json_data = ja.getJSONObject(0);
String mylang=json_data.getString("type");
}
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();
}
}
private static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}catch(IOException e)
{
}
finally
{
try{
is.close();
}catch(IOException e)
{
}
}return sb.toString();
}
}
package com.example.daymanger;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.BufferedHttpEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.widget.TextView;
导入android.widget.Toast;
公共级炼金术扩展了活动范围{
总数;
HttpClient-HttpClient;
HttpPost-HttpPost;
JSONArray arr;
ArrayList名称值对;
HttpResponse响应;
弦长;
http实体;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.alchemy);
TextView al=(TextView)findViewById(R.id.alchemy_text);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
尝试
{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“myurl”);
试一试{
nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“type”,thelangage));
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));
JSONArray ja=jsonResponse.getJSONArray(“doc”);
JSONObject json_data=ja.getJSONObject(0);
String mylang=json_data.getString(“type”);
}
捕获(例外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();
}
}
私有静态字符串convertStreamToString(InputStream为)
{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
尝试
{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常)
{
}
最后
{
试一试{
is.close();
}捕获(IOE异常)
{
}
}使某人返回字符串();
}
}
关键是我想在“doctouction”中获取“type”的值,但它不起作用,有人能帮我吗
doctouction
不是一个JSONArray
它是一个JOSNObject
JSONArray
用[(方括号)表示,JSONObject
用{(大括号)表示
改变
JSONArray ja = jsonResponse.getJSONArray("docSentiment");
到
那就做吧
ja.getString("type");
Scott非常感谢it部门的工作人员—1因为a/没有定义
没有工作
b/没有发布任何日志/堆栈跟踪或其他与解决问题相关的信息。
ja.getString("type");