org.json.JSONException:没有值

时间:2016-03-09 10:47:58

标签: android json retrofit

我在获取json数据时在logcat中遇到此错误

org.json.JSONException:alberrnews没有值

我也使用Retrofit 2而且我不知道我是否以正确的方式发布了baseurl

JsonArray

{"alberrnews":[{"title":"This is a title","subject":"This is a subject","image":"http:\/\/www.vcvcvc.com\/uploaded44\/33707\/61457017225.jpg"}]}

主要课程

public class alberrClassNews extends Fragment {

private RecyclerView recyclerView;
private List<alberrNewsListContent> alberrNews = new ArrayList<alberrNewsListContent>();
private final String TAG = "Get JSONArray";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.alberrnewsclass, container, false);
    getActivity().setTitle("Main Class");

    recyclerView = (RecyclerView) view.findViewById(R.id.alberrNewsList);

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    layoutManager.setReverseLayout(true);
    layoutManager.setStackFromEnd(true);

    final alberrNewsAdapter aNA = new alberrNewsAdapter(getActivity(), alberrNews);
    recyclerView.setAdapter(aNA);


    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("loading");
    progressDialog.show();

    final Retrofit alberrRetrofit = new Retrofit.Builder()
            .baseUrl("http://mm.com/alrass_charties/")
            .addConverterFactory(GsonConverterFactory.create()).build();
    alberrService service = alberrRetrofit.create(alberrService.class);


    Call<JSONObject> call = service.alberrNews();
    call.enqueue(new Callback<JSONObject>() {
        @Override
        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
            progressDialog.dismiss();
            Log.i(TAG, "success");

            try {
                JSONArray jsonArray = response.body().getJSONArray("alberrnews");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    alberrNewsListContent newsContent = new alberrNewsListContent();

                    newsContent.setAlberrNewsTitle(jsonObject.getString("title"));
                    newsContent.setAlberrNewsSubject(jsonObject.getString("subject"));
                    newsContent.setAlberrNewsImage(jsonObject.getString("image"));
                    alberrNews.add(newsContent);
                }


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

        @Override
        public void onFailure(Call<JSONObject> call, Throwable t) {
            Log.i(TAG, "Failed");
            progressDialog.dismiss();

            Toast.makeText(getActivity().getApplicationContext(), "no connection", Toast.LENGTH_SHORT).show();

        }
    });
    return view;

}
}

服务类

public interface alberrService {
@GET("alberr_news.php")
Call <JSONObject> alberrNews();
}
logcat中的

错误

    03-09 04:08:43.617 18686-18696/? I/art: Debugger is no longer active
03-09 04:08:43.656 18686-18686/? I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
03-09 04:08:43.681 18686-18705/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-09 04:08:43.712 18686-18686/? D/Atlas: Validating map...
03-09 04:08:43.765 18686-18705/? D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
03-09 04:08:43.766 18686-18705/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-09 04:08:43.771 18686-18705/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
03-09 04:08:43.812 18686-18705/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-09 04:08:43.857 18686-18705/? D/OpenGLRenderer: Enabling debug mode 0
03-09 04:08:43.890 18686-18705/? W/EGL_emulation: eglSurfaceAttrib not implemented
03-09 04:08:43.890 18686-18705/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3d0fea0, error=EGL_SUCCESS
03-09 04:08:48.667 18686-18705/com.example.abdulmajeed.alrasscharities W/EGL_emulation: eglSurfaceAttrib not implemented
03-09 04:08:48.667 18686-18705/com.example.abdulmajeed.alrasscharities W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3d0eae0, error=EGL_SUCCESS
03-09 04:08:49.164 18686-18686/com.example.abdulmajeed.alrasscharities I/Get JSONArray: success
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err: org.json.JSONException: No value for alberrnews
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at com.example.abdulmajeed.alrasscharities.alberrClassNews$1.onResponse(alberrClassNews.java:74)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:66)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at android.os.Looper.loop(Looper.java:135)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
03-09 04:08:49.165 18686-18686/com.example.abdulmajeed.alrasscharities W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

=============================================== ==================================

1 个答案:

答案 0 :(得分:0)

试试吧

 public interface alberrService {
     @Headers({"content-type: application/json"})
    @GET("alberr_news.php")
    Call <JSONObject> alberrNews();
    }