我很确定这只是一个简单的编码错误。但是我不确定它是什么,这里不允许错误读取Modifier Public。
public class getJsonResponse extends AsyncTask<Void, Void, String> {
String serverUrl;
我正在使用的代码涉及从服务器获取Json,并且当它是一个Activity时代码可以100%工作。但是,当我将活动转换为片段时,它会抛出此错误。任何帮助,将不胜感激。
public class StandingsList extends Fragment {
//make member variable is Views
Button mButton;
TextView mResult;
String JSON_RESPONSE;
ProgressDialog mProgressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate( R.layout.fragment_standings, container, false );
return view;
//get reference of the views
mButton = (Button) view.findViewById( R.id.button );
mResult = (TextView) view.findViewById( R.id.result );
//when button is clicked
mButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
//call the getJsonResponse method and fetch the response from the server
new getJsonResponse().execute();
}
} );
public class getJsonResponse extends AsyncTask<Void, Void, String> {
String serverUrl;
public getJsonResponse() {
mProgressDialog = new ProgressDialog( getActivity() );
mProgressDialog.setMessage( "Please Wait" );
mProgressDialog.setTitle( "Processing" );
mProgressDialog.setCancelable( false );
}
@Override
protected void onPreExecute() {
//set the url from we have to fetch the json response
serverUrl = "http://163.172.142.145/get_info.php";
mProgressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL( serverUrl );
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_RESPONSE = bufferedReader.readLine()) != null) {
stringBuilder.append( JSON_RESPONSE + "\n" );
}
inputStream.close();
bufferedReader.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
Log.e( TAG, "MalformedURLException: " + e ); //print exception message to log
} catch (IOException e) {
Log.e( TAG, "IOException: " + e ); //print exception message to log
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate( values );
}
@Override
protected void onPostExecute(String result) {
//set the result which is returned by doInBackground() method to result textView
mResult.setText( result );
mProgressDialog.dismiss();
}
}
}
}