syntax error
您好,我無法通過Volley請求傳遞參數。 我目前使用GET方法。但是,當我通過帕拉姆顯示「零誤差」無法通過Volley Request中的Params通過android
This the error message picture
我曾嘗試
和波紋管是我的活動代碼。 我試圖通過「dayorder」參數,並在結果中我試圖得到特定的「dayorder's句點」作爲結果數組,並在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);
}
}
謝謝你,我懷疑爲什麼它通過paragm.put傳遞不工作?通過HashMap –
@vigneshs GET和POST參數的發送方式不同。您可以通過在http://example.com/?name=peter&age=27這樣的url中附加GET來發送GET參數。在POST中,您可以在請求體內發送參數。在Volley中,如果您想要將參數添加到GET請求中,請使用字符串連接或格式將它們附加到url中;如果您想要將參數添加到POST請求中,請將參數傳遞到請求構造器或重寫getParams方法。 – FerDensetsu
嘿謝謝你的回覆@FerDensetsu我不知道如何附加GET請求的URL,所以如果我想通過POST請求,我可以通過簡單地將「Request.Method」更改爲POST來實現它嗎? –