Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在两个类之间传递数据_Java_Android_Android Studio 3.0 - Fatal编程技术网

Java 在两个类之间传递数据

Java 在两个类之间传递数据,java,android,android-studio-3.0,Java,Android,Android Studio 3.0,我正在尝试搜索一个信标设备,并获取主要和次要数据,然后将数据发送到请求,但问题是我不知道如何在这两部分之间发送数据。 我想把beacon.getMajor()放在 到jsonObject.put(“uuid”,a) 谢谢大家! 这是我的密码 private List<String> placesNearBeacon(Beacon beacon) { int a = beacon.getMajor(); final String b = String.valueOf(b

我正在尝试搜索一个信标设备,并获取主要和次要数据,然后将数据发送到请求,但问题是我不知道如何在这两部分之间发送数据。
我想把
beacon.getMajor()放在
到
jsonObject.put(“uuid”,a)

谢谢大家!

这是我的密码

private List<String> placesNearBeacon(Beacon beacon) {
    int a = beacon.getMajor();
    final String b = String.valueOf(beacon.getMajor());
    Log.v("show", String.valueOf(a));

    Intent intent = new Intent(this,AsyncLogin.class);
    intent.putExtra("MAJOR",a);

    startActivity(intent);
    if (a == 258){
        new AsyncLogin().execute(String.valueOf(a));
    }
    String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor());
    return Collections.emptyList();
}

enter code here

private class AsyncLogin extends AsyncTask<String, String, String>
{
    HttpURLConnection conn;
    URL url = null;

    Uri a = new Intent().getData();

    @Override
    protected String doInBackground(String... params) {

        try {

            // Enter URL address where your php file resides
            url = new URL("http://etriplay.com/app/beacon_GetInform.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "exception";
        }
        try {
            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");

            // setDoInput and setDoOutput method depict handling of both send and receive
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // JSON

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userid", "membe00001");
            jsonObject.put("mail", "test@mail.com");
            jsonObject.put("uuid", a);

            Log.v("v", String.valueOf(jsonObject));

            // Open connection for sending data
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(String.valueOf(jsonObject));
            writer.flush();
            writer.close();
            os.close();
            conn.connect();
私人列表位置导航(信标){
int a=beacon.getMajor();
最终字符串b=String.valueOf(beacon.getMajor());
Log.v(“show”,String.valueOf(a));
Intent Intent=新的Intent(这是AsyncLogin.class);
意图。额外(“主要”,a);
星触觉(意向);
如果(a==258){
新建AsyncLogin().execute(String.valueOf(a));
}
String beaconKey=String.format(“%d:%d”、beacon.getMajor()、beacon.getMinor());
返回集合。emptyList();
}
在这里输入代码
私有类AsyncLogin扩展了AsyncTask
{
httpurl连接连接;
URL=null;
Uri a=新的意图().getData();
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
//输入php文件所在的URL地址
url=新url(“http://etriplay.com/app/beacon_GetInform.php");
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回“异常”;
}
试一试{
//设置HttpURLConnection类以从php和mysql发送和接收数据
conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“POST”);
//setDoInput和setDoOutput方法描述了发送和接收的处理
conn.setDoInput(真);
连接设置输出(真);
//JSON
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“userid”、“membe00001”);
jsonObject.put(“邮件”test@mail.com");
jsonObject.put(“uuid”,a);
Log.v(“v”,String.valueOf(jsonObject));
//打开用于发送数据的连接
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
writer.write(String.valueOf(jsonObject));
writer.flush();
writer.close();
os.close();
连接();

在本例中,如果您希望将对象或某物放入json,您可以通过以下方式执行ie操作:

try
{
      jsonObject.put("value",chatMessage.getDate());
}
catch (JSONException e)
{
       e.printStackTrace();
}
jsonobject.put(“key”,value);

您可以指定
int值、字符串值、布尔值或任何您想要的值