This my json
{
"user_data":[
{
"year":"2017",
"month":"12",
"day":"12",
"StartTime":"2:00 am",
"Endtime":"4:00 am",
"Hours":"02:00:00"
},
{
"year":"2018",
"month":"12",
"day":"10",
"StartTime":"5:00 am",
"Endtime":"7:00 am",
"Hours":"02:00:00"
}
]
}
i am able to recieve all the data from database using php in android activity,but the problem is i want to send the recieved data to another activity using the intent, i used the for loop to send the recieved data to another activity,when the loop is executed it sends all the data are going to next activity, but when i tried to print them in toast it prints the last rows details, my question is how to set the loop in 2nd activity to recieve one by one of each row.and i didnt set any loop in second activity,because i dont know how to do it. can anyone help me to do it?
this is my android code of 1st activity
package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
public class MainActivity extends Activity {
EditText name, password;
String NAME=null, PASSWORD=null, EMAIL=null;
String Name, Password;
Context ctx=this;
// String[] year ;
// String[] month;
// String[] day;
// String[] StartTime;
// String[] Endtime;
//String[] Hours;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.main_name);
password = (EditText) findViewById(R.id.main_password);
}
public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password);
}
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;
try {
URL url = new URL("http://localhost/sample/FETCH/fetch.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
String err=null;
try
{
JSONObject root = new JSONObject(result);
JSONArray user_data = root.getJSONArray("user_data");
String[] year = new String[user_data.length()];
String[] month = new String[user_data.length()];
String[] day = new String[user_data.length()];
String[] StartTime = new String[user_data.length()];
String[] Endtime = new String[user_data.length()];
String[] Hours = new String[user_data.length()];
for (int i = 0; i < user_data.length(); i++)
{
JSONObject jsonObject = user_data.getJSONObject(i);
year[i]= jsonObject.getString("year");
month[i] = jsonObject.getString("month");
day[i] = jsonObject.getString("day");
StartTime[i] = jsonObject.getString("StartTime");
Endtime[i] = jsonObject.getString("Endtime");
Hours[i] = jsonObject.getString("Hours");
Intent k = new Intent(ctx, Calender.class);
k.putExtra("year", year[i]);
k.putExtra("month", month[i]);
k.putExtra("day", day[i]);
k.putExtra("StartTime", StartTime[i]);
k.putExtra("Endtime", Endtime[i]);
k.putExtra("Hours", Hours[i]);
k.putExtra("err", err);
startActivity(k);
}
//Toast.makeText(MainActivity.this, "result has value" + year[1], Toast.LENGTH_LONG).show();
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result==null)
{
// Toast.makeText(MainActivity.this, year[0], Toast.LENGTH_LONG).show();
}else
{
//Toast.makeText(MainActivity.this, "result has value"+ year[0] , Toast.LENGTH_LONG).show();
}
}
}
}
and this is my second activity,which recieves the data,i didnt set any loop here.
public class Calender extends AppCompatActivity
{
String day, month, year,Hours,Endtime,StartTime, Err,Shours,Sminutes,Ssecond,Ehours,Eminutes,Esecond,Eampm,Sampm;
int s,mo;
public static final int MY_PERMISSIONS_REQUEST_WRITE_CALENDAR = 123;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calender);
context = Calender.this;
writeCalendarEvent();
}
private void writeCalendarEvent()
{
year = getIntent().getStringExtra("year");
month = getIntent().getStringExtra("month");
day = getIntent().getStringExtra("day");
StartTime = getIntent().getStringExtra("StartTime");
Endtime = getIntent().getStringExtra("Endtime");
Hours = getIntent().getStringExtra("Hours");
Err = getIntent().getStringExtra("err");
Toast.makeText(context, "result has value" + year, Toast.LENGTH_LONG).show();
}
}