从mysql数据库将json数组显示到android中

时间:2016-06-08 17:26:36

标签: android mysql json android-fragments

我正在尝试在片段内的RecyclerView上显示我的数据库中的信息。我得到一个跳过布局错误,没有找到协议。

日志

06-08 13:22:14.510 15717-15732/com.example.dennism501.realator W/art: Suspending all threads took: 8.619ms
06-08 13:22:14.511 15717-15732/com.example.dennism501.realator I/art: Background sticky concurrent mark sweep GC freed 4034(784KB) AllocSpace objects, 1(16KB) LOS objects, 54% free, 505KB/1117KB, paused 9.102ms total 29.890ms
06-08 13:22:14.918 15717-15732/com.example.dennism501.realator I/art: Background sticky concurrent mark sweep GC freed 2310(681KB) AllocSpace objects, 0(0B) LOS objects, 42% free, 1076KB/1886KB, paused 5.090ms total 8.824ms
06-08 13:22:14.980 15717-15717/com.example.dennism501.realator W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-08 13:22:15.363 15717-15727/com.example.dennism501.realator W/art: Suspending all threads took: 13.519ms
06-08 13:22:15.369 15717-15732/com.example.dennism501.realator I/art: Background partial concurrent mark sweep GC freed 169(48KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 1847KB/3MB, paused 2.586ms total 123.673ms
06-08 13:22:15.470 15717-15758/com.example.dennism501.realator D/Recycler_state: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
06-08 13:22:15.495 15717-15760/com.example.dennism501.realator D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                 [ 06-08 13:22:15.497 15717:15717 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xf3e34ef0, tid 15717
06-08 13:22:15.513 15717-15717/com.example.dennism501.realator D/Atlas: Validating map...
06-08 13:22:15.761 15717-15760/com.example.dennism501.realator D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
06-08 13:22:15.786 15717-15760/com.example.dennism501.realator D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-08 13:22:15.796 15717-15760/com.example.dennism501.realator D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

                                                                         [ 06-08 13:22:15.803 15717:15760 D/         ]
                                                                         HostConnection::get() New Host Connection established 0xf3e39e90, tid 15760
06-08 13:22:15.823 15717-15760/com.example.dennism501.realator I/OpenGLRenderer: Initialized EGL, version 1.4
06-08 13:22:15.889 15717-15760/com.example.dennism501.realator D/OpenGLRenderer: Enabling debug mode 0
06-08 13:22:15.929 15717-15760/com.example.dennism501.realator W/EGL_emulation: eglSurfaceAttrib not implemented
06-08 13:22:15.929 15717-15760/com.example.dennism501.realator W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3f82060, error=EGL_SUCCESS
06-08 13:22:16.010 15717-15717/com.example.dennism501.realator E/RecyclerView: No adapter attached; skipping layout
06-08 13:22:16.093 15717-15717/com.example.dennism501.realator I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
06-08 13:22:16.306 15717-15717/com.example.dennism501.realator E/RecyclerView: No adapter attached; skipping layout``

现在这里是我的片段asynctask类的代码

public class AsyncHttpTask extends AsyncTask<String, Void, Integer>{


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        getActivity().setProgressBarIndeterminateVisibility(true);
    }

    @Override
    protected Integer doInBackground(String... params) {

        Integer result = 0;
        HttpURLConnection urlConnection;
        try{

            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int statusCode = urlConnection.getResponseCode();

                //checks if the http server request was successful
                if(statusCode == 200){
                    BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while((line = r.readLine()) != null){
                        response.append(line);
                    }

                    parseResult(response.toString());
                    result = 1;

                    System.out.println("Connection esatablishes");

                }else {
                    result = 0;
                    System.out.println(" no Connection esatablishes");
                }

        }catch (Exception e){
            Log.d(TAG, e.getLocalizedMessage());
        }
        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        progressBar.setVisibility(View.GONE);

            if(result == 1){
                setAdaptor();
            }
            else{
                final View view = getActivity().findViewById(R.id.framelayout);
               Snackbar.make(view,"Failed to fetch data",Snackbar.LENGTH_LONG).show();
            }

    }
}

private void parseResult (String result){

    try{

        JSONObject response = new JSONObject(result);
        JSONArray posts = response.optJSONArray("posts");
        feedslist = new ArrayList<>();

            for(int i=0; i<posts.length(); i++){
                JSONObject post = posts.optJSONObject(i);
                Feedlist item = new Feedlist();
                item.setAddress(post.optString("address"));
                item.setPrice(post.optString("price"));
                item.setNbedrooms(post.optString("nbedrooms"));
                item.setNbathrooms(post.optString("nbathrooms"));
                item.setSizeX(post.optString("sizeofpropertyX"));
                item.setSizeY(post.optString("sixeofpropertyY"));
                item.setImage(post.optString("imagepath"));

                feedslist.add(item);

            }

    }catch (JSONException e){
        e.printStackTrace();
    }

}

0 个答案:

没有答案