2013-11-29 3 views
-1

Я пытаюсь получить значение JSon из URL, связанного с Alchemy API в Android, то JSON ответ в браузере:Получение значения из JSON URL Alchemy

{ 

"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», но это не работает, может кто-нибудь мне помочь, пожалуйста

+0

-1 a/not define 'not working' b/не публиковать какой-либо log/stacktrace или другую информацию, относящуюся к решению e вопрос. – njzk2

ответ

0

docSentiment не JSONArray это JOSNObject. JSONArray обозначается [(квадратные скобки) и JSONObject обозначается через {(фигурные скобки)

изменения

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

к

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

затем сделать

ja.getString("type"); 
+0

Скотт благодарит много людей, с которыми он работал –

Смежные вопросы