-1

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/company/terms.html",
    "url": "",
    "language": "english",
    "docSentiment": {
        "type": "negative",
        "score": "-0.423469"
    }
}

アンドロイドの私のコードは次のとおりです。

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」の値を取得したいのですが、機能していません。誰か助けてください

4

1 に答える 1

0

docSentimentJSONArrayではありませんJOSNObjectJSONArray[ (角括弧) でJSONObject示され、{ (中括弧) で示されます。

変化する

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

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

それからする

ja.getString("type");
于 2013-11-29T17:47:39.500 に答える