-1

I'm trying to get a json value from a url related to Alchemy API in android, the json answer in the browser is :

{

 "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"
    }
}

My code in android is :

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();



       }

}

The point is I want to get the value of "type" in "docsentiment" but it's not working, can anyone help me please

Mounzer yaghi
  • 39
  • 1
  • 6
  • -1 for a/ not defining `not working` b/ not posting any log/stacktrace or other information relevant to solving the issue. – njzk2 Nov 29 '13 at 17:56

1 Answers1

0

docSentiment is not a JSONArray it is a JOSNObject. JSONArray is denoted by [ (square brackets) and JSONObject is denoted by { (curly braces)

change

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

to

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

then do

ja.getString("type");
Scott
  • 1,652
  • 1
  • 13
  • 10