1

syntax error
HI i am unable to pass the parameter through Volley Request. i currently use GET Method. But when i pass the param it shows "null error"

This the error message picture

What i have Tried
and bellow is my Activity code. i tried to pass the "dayorder" parameter and in result i tried to get the particular "dayorder's periods" as the result array and print that in the 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);
}

}

2 Answers2

0

In getRequest you provide parameter by Url?"paramKey="value;

new StringRequest(Request.Method.GET, SUBJECT_URL?"KEY_DAYORDER="dayorder,

for Your case

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();
                }
            }) };
Avinash
  • 264
  • 5
  • 15
  • Thanks man i have doubts why it is not working while pass through paragm.put? Through HashMap –  Jul 10 '17 at 15:20
  • @vigneshs GET and POST params are sent differently. You send params in GET by appending them in the url like http://example.com/?name=peter&age=27. In POST, you send params inside the request body. In Volley, if you want to add params to a GET request, you append them in the url using string concatenation or formatting; if you want to add params to a POST request, you pass the params to your request constructor or override getParams method. – FerDensetsu Jul 10 '17 at 16:55
  • hey thanks for your reply @FerDensetsu i don't know how to append with GET request with URL so if i want to pass POST request, can i achieve it by simply changing the "Request.Method" to POST? –  Jul 10 '17 at 17:05
  • or @FerDensetsu can you please give me a piece of code for POST request or a reference for my question so that i can experience who stuff works. I am a beginner to android.... –  Jul 10 '17 at 17:10
  • Here you can find a great answer about the difference between GET and POST when passing parameters https://stackoverflow.com/a/16795805/6828464 – FerDensetsu Jul 10 '17 at 18:32
  • ok i tried the above code@Avinash is shows some syntax error and i am not getting what it is can you please help me with that. –  Jul 11 '17 at 01:38
  • @FerDensetsu i have gone through your thread and i used the GET request as the thread mentioned but still its is showing failure from else part in my code –  Jul 11 '17 at 01:39
  • @vigneshs can u tell me what syntax error u got or can u send me the url link with the parameter – Avinash Jul 11 '17 at 05:36
  • Sorry for late reply@Avinash –  Jul 14 '17 at 14:04
  • I have added the screen shot the syntax error what i am getting –  Jul 14 '17 at 14:10
  • can you send me url and any params that you are passing i will check it – Avinash Jul 14 '17 at 14:37
0
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();
    }

    }

}

HI at last i found what i need and above is the right query. Thanks for every one.