java - 在两个类之间传递数据

标签 java android android-studio-3.0

我正在尝试搜索信标设备并获取 Majoy & Minor,然后将数据发送到请求,但我遇到的问题是我不知道如何在这两个部分之间发送数据..
我想把 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();

最佳答案

在这种情况下,如果你想将对象或其他东西放入 json 中,你可以这样做,即:

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

jsonobject.put("key",value);

您可以分配int 值、string 值、boolean 值或任何您想要的值

关于java - 在两个类之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709419/

相关文章:

java - ViewPager 无法正常显示

java - Spring MVC 抽象 Controller PathVariable

java - 图像是如何渲染到屏幕上的?

android - 如何在 Xamarin 项目中解析 "trust anchor for certification path not found."?

java - 当我使用 FragmentPagerAdapter 将 fragment 添加到 Activity 中时,如何从 Activity 中调用 fragment 方法?

java - 将 json 解析值与用户输入的值进行匹配

android - CardView 中包含的 ListView 未显示全长

android - 请告诉我有关Java移动应用程序的信息

android - 使用 Android Studio 3.0.1 启动 Android 模拟器失败

java - Android Studio 在生成签名 apk 时无法识别 keystore 别名