您好我有一个下载程序类,用于处理我的应用程序中的下载,该类带有上下文。我在我的片段中使用了这个下载程序类,但我不知道在我的片段中新的mDownloader(????, urlAddress, rv, swipeRefreshLayout).execute();
中为上下文传递了什么。当我通过getAcitivity()
HomeFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
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.zoggfrombetelgeuse.closet.MySQL.mDownloader;
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
static String urlAddress = "http://localhost/my-site/dbGet.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
final RecyclerView rv = (RecyclerView) view.findViewById(R.id.card_view);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);
return view;
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new mDownloader(????, urlAddress, rv, swipeRefreshLayout).execute();
}
});
}
}
mDownloader.java
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.zoggfrombetelgeuse.closet.HomeFragment;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.sql.Connection;
public class mDownloader extends AsyncTask<Void, Void, String> {
Context c;
String urlAddress;
RecyclerView rv;
public mDownloader(Context c, String urlAddress, RecyclerView rv, SwipeRefreshLayout swipeRefreshLayout) {
this.c = c;
this.urlAddress = urlAddress;
this.rv = rv;
this.swipeRefreshLayout = swipeRefreshLayout;
}
SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onPreExecute() {
if (!swipeRefreshLayout.isRefreshing()){
swipeRefreshLayout.setRefreshing(true);
}
}
@Override
protected String doInBackground(Void... voids) {
return this.downloadData();
}
@Override
protected void onPostExecute(String jsonData) {
super.onPostExecute(jsonData);
if (jsonData == null){
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(c, "Unable to refresh feed", Toast.LENGTH_SHORT).show();
}
else {
new mDataParser(c,jsonData,rv,swipeRefreshLayout).execute();
}
}
private String downloadData() {
HttpURLConnection con = mConnector.connect(urlAddress);
if (con == null) {
return null;
}
try {
InputStream inputStream = new BufferedInputStream(con.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer jsonData = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) ;
{
jsonData.append(line + "\n");
}
bufferedReader.close();
inputStream.close();
return jsonData.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}}