I have to pass data from fragment to json task and then to servlet. I have included the code of fragment, class and servlet.
public class Home extends Fragment {
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ListView lvAccountSummary;
Bundle bundle = getActivity().getIntent().getExtras();
String userId = bundle.getString("userName");
Toast.makeText(getActivity().getApplicationContext(),userId,Toast.LENGTH_SHORT).show();
View view = inflater.inflate(R.layout.fragment_home,container,false);
lvAccountSummary = (ListView) view.findViewById(R.id.lvAccountSummary);
new JSONTask(view.getContext(), lvAccountSummary).execute("http://localhost:8080/examples/examplesSummary");
return view;
}
}
This is the following JSON task which gets data from the servlet and display on the fragment
public class JSONTask extends AsyncTask<String,String,List<SummaryModel>> {
private ListView lvSummary;
private Context mContext;
private ProgressDialog dialog;
public JSONTask(Context context, ListView lstView){
this.lvSummary = lstView;
this.mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(mContext);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading. Please wait...");
dialog.show();
}
@Override
protected List<SummaryModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url= new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine())!=null){
buffer.append(line);
}
String finalJason = buffer.toString();
JSONObject parentObject = new JSONObject(finalJason);
JSONArray parentArray = parentObject.getJSONArray("account");
List<SummaryModel> accountModelList = new ArrayList<>();
Gson gson = new Gson();
for (int i=0; i<parentArray.length();i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
SummaryModel summaryModel = gson.fromJson(finalObject.toString(),SummaryModel.class);
accountModelList.add(summaryModel);
}
return accountModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<SummaryModel> result) {
super.onPostExecute(result);
dialog.dismiss();
AccountAdapter adapter = new AccountAdapter(mContext,R.layout.row,result);
try {
lvSummary.setAdapter(adapter);
} catch (Exception e){
Toast.makeText(mContext,"No Internet Connection",Toast.LENGTH_SHORT).show();
}
//TODO need to set data to the list
}
}
But when I call userId from servlet I get null value
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//String userName;
System.out.println("++++++++++++++++++++"+(String)request.getParameter("userId"));
...
}