目标检测跟踪 2018-02-21 04:17 采纳率: 66.7%
浏览 2378
已结题

树莓派与Android的一个简单问题,本人小白,解决不了了,求大神帮助!!!

mainactivity

package com.example.rasberrypi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TimerTask;

public class MainActivity extends Activity {
    private Handler mHandler;
    private TextView fire, people;

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

        mHandler = new Handler();
        mHandler.post(new TimerProcess());

        fire = findViewById(R.id.fire);
        people = findViewById(R.id.people);
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String ret = msg.obj.toString();
            switch (msg.what){
                case 1:
                    StringBuilder builder = new StringBuilder();
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(ret);
                        Log.i("json", ""+jsonObject);
                        if (jsonObject.getInt("isFire") == 0) {
                            fire.setText("没有发现火焰");
                            fire.setTextColor(android.graphics.Color.GREEN);
                        } else {
                            fire.setText("发现火焰");
                            fire.setTextColor(android.graphics.Color.RED);
                            mes("火焰传感器探测到火焰!");
                        }
                        if (jsonObject.getInt("isInvaded") == 0) {
                            people.setText("安全");
                            people.setTextColor(android.graphics.Color.GREEN);
                        } else {
                            people.setText("发现入侵者");
                            people.setTextColor(android.graphics.Color.RED);
                            mes("发现有人进入房间!");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                    default:
                        break;
            }
        }
    };

    private void getDevInformation(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String ret = Webconn.getDev();
                System.out.println("get return:");
                System.out.println(ret);
                handler.sendMessage(handler.obtainMessage(1, ret));
            }
        }).start();
    }

    private class TimerProcess implements Runnable{

        @Override
        public void run() {
            mHandler.postDelayed(this, 5000);
            getDevInformation();
        }
    }


    private void mes(String m) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        Notification.Builder builder = new Notification.Builder(MainActivity.this);
        builder.setSmallIcon(R.mipmap.ic_launcher); //设置图标
        builder.setTicker("显示第二个通知");
        builder.setContentTitle("树莓派安防"); //设置标题
        builder.setContentText(m); //消息内容
        builder.setWhen(System.currentTimeMillis()); //发送时间
        builder.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
        builder.setAutoCancel(true);//打开程序后图标消失
        /*空一句*/
        Intent intent = new Intent(MainActivity.this,Activity.class);
        PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        mNotificationManager.notify(1, notification);
    }
}

webconn

 package com.example.rasberrypi;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class Webconn {
    public  static String getDev(){
        String url = "http://192.168.1.104:5000/state/";
        try {
            URL url1 = new URL(url);
            HttpURLConnection urlConnection =(HttpURLConnection) url1.openConnection();
            int code = urlConnection.getResponseCode();
            if(code == 200){
                String strResult = urlConnection.getResponseMessage();
                System.out.println(strResult);
                return strResult;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "null";
    }
}

activity_main

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.rasberrypi.MainActivity">

    <TextView
        android:id="@+id/fire"
        android:layout_width="297dp"
        android:layout_height="95dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.135"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.225" />

    <TextView
        android:id="@+id/people"
        android:layout_width="300dp"
        android:layout_height="88dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="10dp"
        tools:layout_editor_absoluteY="236dp" />

</android.support.constraint.ConstraintLayout>

这里是代码,但是我的运行结果是空白图
图片说明
Android studio中说
mainactivity中间的
jsonObject = new JSONObject(ret);
String ret = Webconn.getDev();
和webconn中的

int code = urlConnection.getResponseCode();
报错
192.168.1.104出来的内容是
图片说明
希望大神可以帮忙解决

  • 写回答

7条回答

  • bdmh 移动开发领域优质创作者 2018-02-22 02:01
    关注

    看具体的错误信息,JSONObject报错,一般都是格式不对

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算