Android 为什么';这个类是否从给定的URL返回JSON数据?
我创建的类不从给定的URL获取数据,我将该URL作为字符串。在静态初始值设定项中,我从字符串生成URL,然后getResponseFromHttpUrl方法应该以字符串形式返回JSON数据。然而,我的Log.d(“jsondata”)根本没有显示任何内容,我的应用程序崩溃了Android 为什么';这个类是否从给定的URL返回JSON数据?,android,Android,我创建的类不从给定的URL获取数据,我将该URL作为字符串。在静态初始值设定项中,我从字符串生成URL,然后getResponseFromHttpUrl方法应该以字符串形式返回JSON数据。然而,我的Log.d(“jsondata”)根本没有显示任何内容,我的应用程序崩溃了 package com.example.android.bakingapp; import android.util.Log; import java.io.IOException; import java.io.Inp
package com.example.android.bakingapp;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class RecipeJson {
public static final String url = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
public static URL urlBuilt;
public static String jsonData;
static {
try {
urlBuilt = buildUrl();
jsonData = getResponseFromHttpUrl(urlBuilt);
Log.d("jsondata", jsonData);
} catch (Exception e) {
e.printStackTrace();
}
}
public static URL buildUrl() throws java.net.MalformedURLException {
return new URL(url);
}
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
}
以下是应用程序日志:
08-07 20:59:54.776 26319-26319/com.example.android.bakingapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.bakingapp, PID: 26319
java.lang.ExceptionInInitializerError
at com.example.android.bakingapp.RecipeListFragment.onCreateView(RecipeListFragment.java:68)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1382)
at android.app.Activity.performStart(Activity.java:7151)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2949)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:194)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:157)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:72)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.example.android.bakingapp.dummy.DummyContent.makeJsonArray(DummyContent.java:55)
at com.example.android.bakingapp.dummy.DummyContent.<clinit>(DummyContent.java:28)
这意味着问题真的出在我的班上。但问题的根源是什么
编辑:这是RecipeListFragment代码
package com.example.android.bakingapp;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.bakingapp.dummy.DummyContent;
import com.example.android.bakingapp.dummy.DummyContent.DummyItem;
/**
* A fragment representing a list of Items.
* <p/>
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
* interface.
*/
public class RecipeListFragment extends Fragment {
private static final String ARG_COLUMN_COUNT = "column-count";
int dp;
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public RecipeListFragment() {
}
public static RecipeListFragment newInstance(int columnCount) {
RecipeListFragment fragment = new RecipeListFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
dp = config.smallestScreenWidthDp;
if (dp < 600) mColumnCount = 1;
else mColumnCount = 3;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recipelist_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyRecipeListRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
void onListFragmentInteraction(DummyItem item);
}
}
package com.example.android.bakingapp;
导入android.content.Context;
导入android.content.res.Configuration;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v7.widget.GridLayoutManager;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入com.example.android.bakingapp.dummy.dummcontent;
导入com.example.android.bakingapp.dummy.DummyContent.DummyItem;
/**
*表示项目列表的片段。
*
*包含此片段的活动必须实现{@link OnListFragmentInteractionListener}
*接口。
*/
公共类RecipeListFragment扩展了片段{
私有静态最终字符串ARG\u COLUMN\u COUNT=“COLUMN COUNT”;
int-dp;
私有int mColumnCount=1;
私有OnListFragmentInteractionListener-MLListener;
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公共RecipeListFragment(){
}
公共静态RecipeListFragment newInstance(int columnCount){
RecipeListFragment=新RecipeListFragment();
Bundle args=新Bundle();
args.putInt(ARG\u COLUMN\u COUNT,columnCount);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
配置配置=getResources().getConfiguration();
dp=config.smallestScreenWidthDp;
如果(dp<600)mColumnCount=1;
else McColumnCount=3;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u recipelist\u列表,容器,false);
//设置适配器
如果(查看RecyclerView的实例){
Context=view.getContext();
RecyclerView RecyclerView=(RecyclerView)视图;
如果(mColumnCount您可以发布RecipeListFragment代码吗?:)静态中的API方法调用?@egoldx有问题吗?@divyansuvarma您正在使用哪个Android版本进行测试?在模拟器上测试Android P。
package com.example.android.bakingapp;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.bakingapp.dummy.DummyContent;
import com.example.android.bakingapp.dummy.DummyContent.DummyItem;
/**
* A fragment representing a list of Items.
* <p/>
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
* interface.
*/
public class RecipeListFragment extends Fragment {
private static final String ARG_COLUMN_COUNT = "column-count";
int dp;
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public RecipeListFragment() {
}
public static RecipeListFragment newInstance(int columnCount) {
RecipeListFragment fragment = new RecipeListFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
dp = config.smallestScreenWidthDp;
if (dp < 600) mColumnCount = 1;
else mColumnCount = 3;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recipelist_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyRecipeListRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
void onListFragmentInteraction(DummyItem item);
}
}