无法在android中通过截击请求传递参数
无法在android中通过截击请求传递参数,android,hashmap,android-volley,http-get,Android,Hashmap,Android Volley,Http Get,嗨,我无法通过截击请求传递参数。 我目前使用GET方法。但当我传递参数时,它显示“null error” 我尝试过的 下面是我的活动代码。 我尝试传递“dayorder”参数,结果我尝试获取特定的“dayorder的时段”作为结果数组,并在TextView中打印它 public class AttendanceActivity extends AppCompatActivity { private TextView today; private TextView todayDate; pri
嗨,我无法通过截击请求传递参数。 我目前使用GET方法。但当我传递参数时,它显示“null error” 我尝试过的
下面是我的活动代码。 我尝试传递“dayorder”参数,结果我尝试获取特定的“dayorder的时段”作为结果数组,并在TextView中打印它
public class AttendanceActivity extends AppCompatActivity {
private TextView today;
private TextView todayDate;
private TextView textViewResult;
private String dayorder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
today = (TextView) findViewById(R.id.textDay);
todayDate = (TextView) findViewById(R.id.textDate);
textViewResult=(TextView)findViewById(R.id.textViewResult);
chechDate();
}
public void chechDate(){
Calendar rightNow = Calendar.getInstance();
if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
today.setText("Monday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
today.setText("Tuesday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY){
today.setText("Wednesday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
today.setText("Thursday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
today.setText("Friday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
today.setText("Saturday");
} else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
today.setText("Sunday");
}else{
today.setText("Unable to get day");
}
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
dayorder=today.toString();
todayDate.setText(formattedDate);
getData();
}
private void getData() {
dayorder = today.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.GET, SUBJECT_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
showJSON(response);
} else {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AttendanceActivity.this, "Unable to connect. Please connect to Internet!", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams(){
Map<String, String> map = new HashMap<>();
map.put(KEY_DAYORDER, dayorder);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String period1="";
String period2="";
String period3="";
String period4="";
String period5="";
String period6="";
String period7="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_SUBJECTARRAY);
JSONObject collegeData = result.getJSONObject(0);
period1 = collegeData.getString(Config.KEY_PERIOD1);
period2 = collegeData.getString(Config.KEY_PERIOD2);
period3 = collegeData.getString(Config.KEY_PERIOD3);
period4 = collegeData.getString(Config.KEY_PERIOD4);
period5 = collegeData.getString(Config.KEY_PERIOD5);
period6 = collegeData.getString(Config.KEY_PERIOD6);
period7 = collegeData.getString(Config.KEY_PERIOD7);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText(period1+period2+period3+period4+period5+period6+period7);
}
公共课堂出席活动扩展了AppCompative活动{
今日私人文本视图;
私有文本视图todayDate;
私有文本视图文本视图结果;
私有字符串日顺序;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u考勤);
今天=(TextView)findViewById(R.id.textDay);
todayDate=(TextView)findViewById(R.id.textDate);
textViewResult=(TextView)findViewById(R.id.textViewResult);
chechDate();
}
公共无效支票(){
Calendar rightNow=Calendar.getInstance();
if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.MONDAY){
今天.setText(“星期一”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.周二){
今天。setText(“星期二”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.周三){
今天。setText(“星期三”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.周四){
今天。setText(“星期四”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.FRIDAY){
今天。setText(“星期五”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.SATURDAY){
今天.setText(“星期六”);
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.SUNDAY){
今天。setText(“星期日”);
}否则{
today.setText(“无法获取日期”);
}
Calendar c=Calendar.getInstance();
System.out.println(“当前时间=>”+c.getTime());
SimpleDataFormat df=新的SimpleDataFormat(“dd MMM yyyy”);
字符串formattedDate=df.format(c.getTime());
dayorder=today.toString();
todayDate.setText(格式化日期);
getData();
}
私有void getData(){
dayorder=today.getText().toString().trim();
StringRequest StringRequest=新的StringRequest(Request.Method.GET,SUBJECT\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
if(response.trim().equals(“success”)){
Toast.makeText(AttendanceActivity.this、response、Toast.LENGTH_LONG.show();
showJSON(响应);
}否则{
Toast.makeText(AttendanceActivity.this、response、Toast.LENGTH_LONG.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(AttendanceActivity.this,“无法连接。请连接到Internet!”,Toast.LENGTH\u LONG.show();
}
}) {
@凌驾
受保护的映射getParams(){
Map Map=newhashmap();
图.put(键_DAYORDER,DAYORDER);
返回图;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void showJSON(字符串响应){
字符串周期1=“”;
字符串周期2=“”;
字符串周期3=“”;
字符串周期4=“”;
字符串周期5=“”;
字符串周期6=“”;
字符串周期7=“”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(Config.JSON_SUBJECTARRAY);
JSONObject collegeData=result.getJSONObject(0);
period1=collegeData.getString(Config.KEY\u period1);
period2=collegeData.getString(Config.KEY\u period2);
period3=collegeData.getString(Config.KEY\u period3);
period4=collegeData.getString(Config.KEY\u period4);
period5=collegeData.getString(Config.KEY\u period5);
period6=collegeData.getString(Config.KEY\u period6);
period7=collegeData.getString(Config.KEY\u period7);
}捕获(JSONException e){
e、 printStackTrace();
}
textViewResult.setText(period1+period2+period3+period4+period5+period6+period7);
}
}在getRequest中,您通过Url提供参数?“paramKey=”value 为了你的案子
StringRequest stringRequest = new StringRequest(Request.Method.GET, SUBJECT_URL?"KEY_DAYORDER="dayorder,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
showJSON(response);
} else {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AttendanceActivity.this, "Unable to connect. Please connect to Internet!", Toast.LENGTH_LONG).show();
}
}) };
StringRequest StringRequest=newstringrequest(Request.Method.GET,SUBJECT\u URL?)KEY\u DAYORDER=“DAYORDER,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
if(response.trim().equals(“success”)){
Toast.makeText(AttendanceActivity.this、response、Toast.LENGTH_LONG.show();
showJSON(响应);
}否则{
Toast.makeText(AttendanceActivity.this、response、Toast.LENGTH_LONG.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(AttendanceActivity.this,“无法连接。请连接到Internet!”,Toast.LENGTH\u LONG.show();
}
}) };
在getRequest中,您通过Url提供参数?“paramKey=”value
为了你的案子
StringRequest stringRequest = new StringRequest(Request.Method.GET, SUBJECT_URL?"KEY_DAYORDER="dayorder,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
showJSON(response);
} else {
Toast.makeText(AttendanceActivity.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AttendanceActivity.this, "Unable to connect. Please connect to Internet!", Toast.LENGTH_LONG).show();
}
}) };
StringRequest StringRequest=newstringrequest(Request.Method.GET,SUBJECT\u URL?)KEY\u DAYORDER=“DAYORDER,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
if(response.trim().equals(“success”)){
Toast.makeText(AttendanceActivity.this、response、Toast.LENGTH_LONG.show();
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.Calendar;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import static com.example.vicky.module1.Config.KEY_DAYORDER;
import static com.example.vicky.module1.Config.KEY_PERIOD1;
import static com.example.vicky.module1.Config.SUBJECT_URL;
public class AttendanceActivity extends AppCompatActivity {
private TextView today;
private TextView todayDate;
private TextView textViewResult;
private String dayorder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
today = (TextView) findViewById(R.id.textDay);
todayDate = (TextView) findViewById(R.id.textDate);
textViewResult=(TextView)findViewById(R.id.textViewResult);
chechDate();
}
public void chechDate(){
Calendar rightNow = Calendar.getInstance();
if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
today.setText("Monday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
today.setText("Tuesday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY){
today.setText("Wednesday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
today.setText("Thursday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
today.setText("Friday");
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
today.setText("Saturday");
} else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
today.setText("Sunday");
}else{
today.setText("Unable to get day");
}
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
todayDate.setText(formattedDate);
getData();
}
private void getData(){
dayorder = today.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, SUBJECT_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(AttendanceActivity.this,response,Toast.LENGTH_LONG).show();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AttendanceActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_DAYORDER, dayorder);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String period1="";
String period2="";
String period3="";
String period4="";
String period5="";
String period6="";
String period7="";
try {
Toast.makeText(AttendanceActivity.this, "showjson try method", Toast.LENGTH_SHORT).show();
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_SUBJECTARRAY);
JSONObject collegeData = result.getJSONObject(0);
period1 = collegeData.getString(KEY_PERIOD1);
textViewResult.setText(period1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}