从url Alchemy获取json值

时间:2013-11-29 17:44:04

标签: android json alchemyapi

我正在尝试从Android中与Alchemy API相关的网址获取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/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();



       }

}

关键是我想在“docsentiment”中获得“type”的价值,但是它没有用,有人可以帮我吗

1 个答案:

答案 0 :(得分:0)

docSentiment不是JSONArray,而是JOSNObjectJSONArray用[(方括号)表示,JSONObject用{(花括号)表示

变化

JSONArray ja = jsonResponse.getJSONArray("docSentiment");

JSONObject ja = jsonResponse.getJSONObject("docSentiment");

然后做

ja.getString("type");