Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t在EditText中显示转换后的JSONObject_Java_Android - Fatal编程技术网

Java Can';t在EditText中显示转换后的JSONObject

Java Can';t在EditText中显示转换后的JSONObject,java,android,Java,Android,我试图在android应用程序的EditText字段中显示已转换为字符串的JSONObject。当我运行应用程序时,它会无误加载,但我的EditText字段为空。我还试着做System.out.println(name);但是没有打印到控制台上,所以我最好的猜测是,控制台上实际上没有存储任何内容,我不确定原因。 这是我的主要文件 package com.apitest.rottentomatoestest; import android.os.Bundle; import java.u

我试图在android应用程序的EditText字段中显示已转换为字符串的JSONObject。当我运行应用程序时,它会无误加载,但我的EditText字段为空。我还试着做System.out.println(name);但是没有打印到控制台上,所以我最好的猜测是,控制台上实际上没有存储任何内容,我不确定原因。
这是我的主要文件

package com.apitest.rottentomatoestest;

import android.os.Bundle;    
import java.util.ArrayList;
import java.util.HashMap;    
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;    
import org.json.*;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;    
import android.widget.TextView;
import android.widget.EditText;
import java.util.logging.*;

public class Main extends Activity {        

    private static String url = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpd6mk8fnncf67";

    private static final String TAG_CAST = "cast";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_CHARACTERS = "characters";

    EditText display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainlayout);
            JSONArray cast = null; 
            JSONParser jParser = new JSONParser();
            JSONObject jSon = jParser.getJSONFromUrl(url);              
            display = (EditText) findViewById(R.id.display);            

            try
            {
                cast = jSon.getJSONArray(TAG_CAST);                 
                for(int i=0; i < cast.length(); i++){
                    JSONObject c = cast.getJSONObject(i);                       
                    String name = c.getString(TAG_NAME);                        
                    display.setText(name); 
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        catch (Exception e){
            Log.e("ERROR", "ERROR IN CODE" + e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mainlayout, menu);
        return true;
    }
}
cast=jSon.getJSONArray(TAG_cast);
对于(int i=0;i

您最好检查
name

的最后一个值。您是否在打印服务器的响应时检查了它?确保您从服务器获得有效响应实际上给定的url返回json
{“error”:“notauthorized”}
这就是原因吗?我尝试通过System.out.println(name)检查name的最后一个值,但这不会将任何内容打印到控制台。是否有其他方法可以检查值?在LogCat中,我得到了一个NullPointerException错误和一个解析数据的错误,该数据读取的输入末尾为的字符0。这之后就没有什么了。如何检查我是否收到服务器的响应?能否粘贴在class
JSONParser
中获得的
json
字符串值?给定的url返回
{“error”:“Not Authorized”}
以下url对我有效:当我在浏览器中输入该url时,它将正常加载。我已检查了您的代码并获得了值。检查
httpClient.execute(httpPost)
,是否存在抛出异常
unknownhostexception
?也许你忘了权限了我昨晚抓到了一个未知的例外。我授予应用程序权限,然后它开始抛出以下错误。分析数据JSONException时出错:无法将java.lang.String类型的值596转换为JSONObejct。代码NullPointerException中出错。
package com.apitest.rottentomatoestest;

import java.io.*;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;

import android.util.Log;

public class JSONParser {

    static InputStream inputStream = null;
    static JSONObject jObject = null;
    static String jSon = "";

    public JSONParser() {
        // TODO Auto-generated constructor stub
    }

    public JSONObject getJSONFromUrl(String url){

        //Make HTTP Request
        try {
            //defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            inputStream = httpEntity.getContent();

        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (ClientProtocolException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null){
                stringBuilder.append(line + "\n");
            }
            inputStream.close();
            jSon = stringBuilder.toString();
        } catch (Exception e){
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        //try to parse the string to JSON Object
        try {
            jObject = new JSONObject(jSon);
        } catch (JSONException e){
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        //return JSON String
        return jObject;
    }    
}
 cast = jSon.getJSONArray(TAG_CAST);

  for(int i=0; i < cast.length(); i++){
       JSONObject c = cast.getJSONObject(i);

        String name = c.getString(TAG_NAME);

        display.setText(name);
}