有人能帮助我吗?什么错了我Volley获取请求

时间:2016-12-29 20:19:48

标签: java android request android-volley

Score.java 我的代码有问题,有人可以帮帮我吗?

public class Score extends AppCompatActivity {
    public static String param2;
    public String param1;
    private ProgressDialog pDialog;
    private SessionManager session;
    private SQLiteHandler db;
    String id;
    int n=0;
    private ArrayList<Logsoal> logsoal = new ArrayList<Logsoal>();
    private static String TAG = Score.class.getSimpleName();
    private String urlJsonObj = "http://192.168.43.97:80/sbmptn/web/api/historipengerjaan";
    private String getIdhistori = "http://192.168.43.97:80/sbmptn/web/api/getidhistorypengerjaan";
    private String sendlog = "http://192.168.43.97:80/sbmptn/web/api/logsoal";
    String date,time;
    int id_historypengerjaan=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        // Progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);

        // Session manager
        session = new SessionManager(getApplicationContext());

        // SQLite database handler
        db = new SQLiteHandler(getApplicationContext());

        HashMap<String, String> user = db.getUserDetails();

        id = user.get("id_user");

        Intent i = getIntent();
        Bundle bundle = i.getExtras();
        param1 = bundle.getString("id_bab");
        param2 = bundle.getString("judul");
        n = i.getIntExtra("nilai",0);
        logsoal = (ArrayList<Logsoal>) bundle.getSerializable("log");
        Log.d("Test Log Soal", logsoal+"");
        change_image(n);

    }

    public void goToMain (View view){
        Intent intent = new Intent(this, MenuPage.class);
        intent.putExtra("id_bab",param1);
        intent.putExtra("judul",param2);
        startActivity(intent);
        finish();
    }

    public void goToPembahasan (View view) {
        ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (session.isLoggedIn()) {
                Calendar c1 = Calendar.getInstance();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdft = new SimpleDateFormat("HH:mm:ss");
                date = sdf1.format(c1.getTime());
                time = sdft.format(c1.getTime());
                String timetemp = time;
                Log.d("waktu1", time);
                urlJsonObj = "http://192.168.43.97:80/sbmptn/web/api/historipengerjaan?id_user=" + id + "&score=" + n + "&tanggal_pengerjaan=" + date + "&waktu_pengerjaan=" + time;
                Log.d("waktu2", timetemp);
                pDialog.setMessage("Send Data ...");
                showDialog();

                registerHistori();
                getIdhistori = "http://192.168.43.97:80/sbmptn/web/api/getidhistorypengerjaan?tgl=" + date + "&waktu=" + timetemp";

                makeJsonObjectRequest();
                Log.d(TAG, "id histori: "+id_historypengerjaan +" tanggal : "+date+" tanggal : "+time);


                hideDialog();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_LONG)
                        .show();
            }
            Intent intent = new Intent(this, pembahasan_soal.class);
            intent.putExtra("id_bab", param1);
            intent.putExtra("judul", param2);
            startActivity(intent);
            finish();
        }else{
            Toast.makeText(getApplicationContext(), "You Must Be Online To Next Access", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, MenuPage.class);
            intent.putExtra("id_bab", param1);
            intent.putExtra("judul", param2);
            startActivity(intent);
            finish();
        }
    }

    public void change_image(int point)
    {
        ImageView myPicture=(ImageView)findViewById(R.id.my);
        TextView myText =(TextView)findViewById(R.id.scoreText);
        //use flag to change image
        if(point <= 30)
        {
            myPicture.setImageResource(R.drawable.sad);
            myText.setText("Score Kamu adalah: "+point);
        }
        else if(point>30 && point<=70)
        {
            myPicture.setImageResource(R.drawable.happy1);
            myText.setText("Score Kamu adalah: "+point);
        }
        else if(point>70 && point <=100){
            myPicture.setImageResource(R.drawable.inlove);
            myText.setText("Score Kamu adalah: "+point);
        }
        else{
            myPicture.setImageResource(R.drawable.design);
            myText.setText("Selamat..\nScore Kamu adalah: "+point);
        }
    }

    private void registerHistori() {
        // Tag used to cancel the request
        String tag_string_req = "req_register";

        JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST, urlJsonObj, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d("Test", "Register Response: " + response.toString());
                hideDialog();

                try {
                    String status = response.getString("status");
                    if (status.equals("1")) {
                        // Launch login activity
                    } else {

                        // Error occurred in registration. Get the error
                        // message
                        String errorMsg = response.getString("Error Please try again");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("erorr :", "Input data Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        });
        AppController.getInstance().addToRequestQueue(strReq);
    }

    private void makeJsonObjectRequest() {
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,getIdhistori,null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JSONArray setdata = new JSONArray();
                Log.d("status", "onResponse: "+ response.toString());
                try {
                    String status = response.getString("status");
                    if(status.equals("1"))
                        setdata = response.getJSONArray("data");
                    for (int i = 0; i < setdata.length(); i++) {
                        JSONObject data = setdata.getJSONObject(i);
                        id_historypengerjaan = data.getInt("id");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hideDialog();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
        for (int j=0 ; j < logsoal.size();j++){
            sendlog = "http://192.168.43.97:80/sbmptn/web/api/logsoal?id_user=" + id + "&id_soal=" + logsoal.get(j).getId_soal() + "&id_jawabsoal=" + logsoal.get(j).getId_jawabsoal() + "&id_historyPengerjaan=" + id_historypengerjaan;
            Log.d(TAG, "log soal: idsoal : "+logsoal.get(j).getId_soal()+" id_jawab : "+logsoal.get(j).getId_jawabsoal()+" id_histori : "+id_historypengerjaan);
            registerLogsoal();
        }
    }

    private void registerLogsoal() {
        String tag_string_req = "req_register";

        JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST, sendlog, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d("Test", "Register Response: " + response.toString());
                hideDialog();

                try {
                    String status = response.getString("status");
                    if (status.equals("1")) {
                        // Launch login activity
                    } else {

                        // Error occurred in registration. Get the error
                        // message
                        String errorMsg = response.getString("Error Please try again");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("erorr :", "Input data Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        });
        AppController.getInstance().addToRequestQueue(strReq);
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}

logcat og makejsonobjectrequest enter image description here

我想在方法makejsonobjectrequets中获取此API:{&#34; status&#34;:&#34; 1&#34;,&#34;数据&#34;:[{&#34 ; id&#34;:48}]} 但结果总是错误的,通过参数是正确的 任何人都可以帮助我吗?

posman sc

1 个答案:

答案 0 :(得分:2)

如您提供的Postman请求快照中所示,您忘记在URL中附加参数。参数是

  

TGL

  

waktu

以下是您应该如何附加参数。

private void makeJsonObjectRequest() {

    //Here param1 and param2 should be appropriate parameters.
    getIdhistori=getIdhistory+"tgl="+param1+"&waktu="+param2;

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,getIdhistori,null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JSONArray setdata = new JSONArray();
                Log.d("status", "onResponse: "+ response.toString());
                try {
                    String status = response.getString("status");
                    if(status.equals("1"))
                        setdata = response.getJSONArray("data");
                    for (int i = 0; i < setdata.length(); i++) {
                        JSONObject data = setdata.getJSONObject(i);
                        id_historypengerjaan = data.getInt("id");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hideDialog();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
        for (int j=0 ; j < logsoal.size();j++){
            sendlog = "http://192.168.43.97:80/sbmptn/web/api/logsoal?id_user=" + id + "&id_soal=" + logsoal.get(j).getId_soal() + "&id_jawabsoal=" + logsoal.get(j).getId_jawabsoal() + "&id_historyPengerjaan=" + id_historypengerjaan;
            Log.d(TAG, "log soal: idsoal : "+logsoal.get(j).getId_soal()+" id_jawab : "+logsoal.get(j).getId_jawabsoal()+" id_histori : "+id_historypengerjaan);
            registerLogsoal();
        }
    }
祝你好运!