1

Here is my code for "MainActivity.java", I can't post JSONObject to the API server & also can't get the response data inside the simple Textview. Please Help. I am new to Java

When I am trying to build the project it is not showing anything

http://www.zoftino.com/get-&-post-data-using-http-library-volley-in-android

Reference link is given above.

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button getApiBtn, postApiBtn;
    private TextView resultTextView;
    RequestQueue requestQueue;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultTextView = (TextView) findViewById(R.id.resultTextView);
        getApiBtn = (Button) findViewById(R.id.getApiBtn);
        postApiBtn = (Button)findViewById(R.id.postApiBtn);

        getApiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

        postApiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postData();
            }
        });
    }

    // methods are here for post and get
    public void postData() {

        try {
            String url = getResources().getString(R.string.url);
            JSONObject object = new JSONObject();
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    resultTextView.setText("Data Posted");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("user_Name", "Faruk");
                    params.put("pas_sword", "1234567");
                    return params;
                }
            };
            requestQueue.add(jsonObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getData(){
        try {
            String url = getResources().getString(R.string.url);
            JSONObject object = new JSONObject();
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    resultTextView.setText("Resposne : " + response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
SPurno
  • 149
  • 1
  • 11

2 Answers2

1

you forgot to Instantiate the RequestQueue .

see this https://developer.android.com/training/volley/simple#java for more clarity

Srinivasan M
  • 317
  • 1
  • 3
  • 11
1

I have gone through over the Blog you have mentioned. There's one thing missed that is internet permission inAndroidManifest.xml. Please add this <uses-permission android:name="android.permission.INTERNET"/> permission to your manifest and try again.

If something else causing the problem please comment your problem clearly. Is your key name for postData method is correct? pas_sword

Al-Amin
  • 1,369
  • 1
  • 9
  • 26
  • Yes I have used both android.permission.INTERNET & NETWORK_STATE – SPurno Oct 25 '18 at 10:06
  • Got a problem of your code - You didn't initialize `RequestQueue requestQueue;` – Al-Amin Oct 25 '18 at 10:11
  • 1
    No you didn't. You just declared a variable. Add this line of code in your `onCreate` method. `requestQueue = Volley.newRequestQueue(getApplicationContext());` – Al-Amin Oct 25 '18 at 10:17
  • I am still now in Android Studio. After declaring inside onCreate method RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); the application is stopped working. – SPurno Oct 25 '18 at 10:20