[英]How to pass data dynamically from array to JSON and show it in a toast using Android
Please help me how to put data to JSON and toast it when I press share key in my code. 当我在代码中按共享键时,请帮助我如何将数据放入JSON并进行烘烤。 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 . 我无法通过此put方法传递医师密钥值,即使我将其存储在字符串中并通过它,医师密钥也没有显示。
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();
}
});
You are not performing any HTTP requests. 您没有执行任何HTTP请求。 Create an instance of the following class passing your url, then call GetJsonData() inside your Toast: 创建以下类的实例来传递您的网址,然后在Toast中调用GetJsonData():
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: 然后,您可以将字符串转换为JSON对象并执行所需的操作:
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 您可能会发现这很有用: 在Android上从URL简单解析JSON并显示在列表视图中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.