Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting json value from url Alchemy
    primarykey
    data
    text
    <p>I'm trying to get a json value from a url related to Alchemy API in android, the json answer in the browser is :</p> <pre><code>{ "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" } } </code></pre> <p>My code in android is :</p> <pre><code>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&lt;NameValuePair&gt; 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&lt;NameValuePair&gt;(); 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(); } } </code></pre> <p>The point is I want to get the value of "type" in "docsentiment" but it's not working, can anyone help me please</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload