【问题标题】:How to implement a JSON handler/helper class to handle JSONrequest on Android Studio [duplicate]如何在 Android Studio 上实现 JSON 处理程序/帮助程序类来处理 JSONrequest [重复]
【发布时间】:2017-10-23 02:42:30
【问题描述】:

我需要访问托管在网络上某处的 JSON,检索并操作它。 为此,我知道建议实现一个处理程序类来帮助处理 JSON 调用,但我不记得操作方法了。我记得你可以扩展 AsyncTask 并使用 String、Void 和 JSONObject 作为通配符,你知道,像这样:

public class JSONhandler extends AsyncTask <String, Void, JSONObject>{
     //code here
}

我想通过扩展 AsyncTask 来严格做到这一点。我知道question 解决了 AsyncTask 的使用问题,但这不是我想要的。该问题的代码扩展了 Activity,我不知道哪个类是 Android Activity,我想在 Android Activity 中实现这个类。

问题是我不记得操作方法了。

提前致谢。

【问题讨论】:

    标签: android json


    【解决方案1】:

    有很多方法可以做到这一点,如果你想使用 AsyncTask,你的类中的代码应该如下

    public class JSONrequest extends AsyncTask<String, Void, JSONObject> {
    
        // First you'll need an interface that'll call requestDone() for the JSONobject you wanna listen
        public interface JSONListener{
            void requestDone(JSONObject jsonObject);
        }
    
        private JSONListener jListener;
    
        //Your class will need a constructor. Basic Java.
        public JSONrequest(JSONListener jListener){
            this.jListener = jListener;
        }
    
        @Override
        //You'll be required to implement this method because of the extension and
        //here's where the logic of your handler class goes
        protected JSONObject doInBackground(String... strings) {
    
        // First try to retrieve the url
        JSONObject result = null;  //initializing the result we'll return
        try{
            URL url = new URL(strings[0]);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
    
    
            int responseCode = urlConnection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
    
                InputStream is = urlConnection.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String currentLine = "";
    
                while((currentLine = br.readLine()) != null){
                    Log.d("JSON REQUEST", currentLine); //a little log so you can see what's going on
                    sb.append(currentLine);
                }
    
                result = new JSONObject(sb.toString());
    
            }
    
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
            return null;
        }
    }
    

    如果您要从服务器获取 JSON,授予您的应用使用 Internet 的权限非常重要。 为此,您需要在 Android Manifest 中添加以下行:

    <uses-permission android:name="android.permission.INTERNET" />
    

    我希望这能解决您的问题。 祝你好运。

    【讨论】:

    • 谢谢!我对我的活动类进行了完美的处理,结果如下: public class Activity extends AppCompatActivity implements JSONrequest.JSON.JSONListener { //Override protected void onCreate(Bundle savedInstanceState) { //onCreate and setContentView stuff JSONrequest req = new JSONrequest(this ); req.execute("我获取 JSON 的 url"); } //重写 public void requestDone(){ try{ //我的逻辑 }catch(JSONExeption je){ je.printStack(); } } }
    猜你喜欢
    • 2011-09-21
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2011-03-03
    • 2020-12-10
    • 2019-09-16
    • 1970-01-01
    相关资源
    最近更新 更多