0

Please help me how to put data to JSON and toast it when I press share key in my code. My problem is when I press the share button it toasts:

Physician key :
patient key :5010

I am not able to pass the physician key value in this put method and my physician key is also not showing even though I store it in a string and pass it .

public class SendPostRequest extends AsyncTask<String, Void, String> {

        protected void onPreExecute(){}

        protected String doInBackground(String... arg0) {

            try {

                URL url = new URL("xyz.com/data/abc.physicinlist"); // here is your URL path

                final JSONObject postDataParams = new JSONObject();
              /*  postDataParams.put("name", "abc");
                postDataParams.put("email", "abc@gmail.com");*/

                // Getting JSON Array node
                JSONArray ary = postDataParams.getJSONArray("physicianlist");


                    ***postDataParams.put("physiciankey", Physician_key);
                    postDataParams.put("PatientKey", "5010");***

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, postDataParams.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
Darush
  • 11,403
  • 9
  • 62
  • 60

1 Answers1

0

You are not performing any HTTP requests. Create an instance of the following class passing your url, then call GetJsonData() inside your Toast:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

public class GetJsonFromUrl {
    String url = null;

    public GetJsonFromUrl(String url) {
        this.url = url;
    }

    public String GetJsonData() {
        try {
            URL Url = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            line = sb.toString();
            connection.disconnect();
            is.close();
            sb.delete(0, sb.length());
            return line;
        } catch (Exception e) {
            return null;
        }
    }
} 

This gives you the response string. You can then convert your string into JSON object and do what you want:

GetJsonFromUrl yourObject = new GetJsonFromUrl("xyz.com/data/abc.physicinlist");
JSONObject jsonObject = new JSONObject(yourObject.GetJsonData());

You may find this helpful: Simple parse JSON from URL on Android and display in listview

Darush
  • 11,403
  • 9
  • 62
  • 60