网站上发布了一个JSON,我试图阅读并回到Android应用程序通知中。
有问题的JSON字符串: {" fridgestatus":"失败"," fridedetail":[{" unit":" zxcv",&# 34;姓名":" Chill Wall#2"," readtime":" 2018-06-05T16:57:00.789000Z"," TempF":27.14},{" unit":" asdf"," name":" Chill Wall#3",&# 34; readtime":" 2018-06-05T17:41:08.961000Z"," TempF":37.22},{" unit":&#34 ; qwer"," name":" Chill Wall#1"," readtime":" 2018-06-05T17:30:50.822000Z& #34;," TempF":33.98},{" unit":" tyui"," name":" Back Room "," readtime":" 2018-06-05T18:34:52.605000Z"," TempF":49.46},{" unit& #34;:" ghjk"," name":" Chill Wall#5"," readtime":" 2018-06 -05T18:23:17.123000Z"," TempF":36.68}]}
这是托管在某个地址的网址上。
这是我在Java中的代码:
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class main {
public static void main(String[] args) throws Exception{
String fridge = "All fridges are normal";
fridge = getFridge();
System.out.print(fridge);
}
public static String getText(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"UTF8"));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
public static JSONObject parser() throws Exception{
JSONObject json = new JSONObject(getText("WEB URL"));
return json;
}
public static String getFridge() throws Exception{
String thisFridge = "No issues.";
JSONObject json = parser();
JSONArray n = json.getJSONArray("fridgedetail");
for (int i=0; i<n.length(); i++){
JSONObject m = n.getJSONObject(i);
if(m.getDouble("TempF")>40.00)
thisFridge = m.getString("name");
}
return thisFridge;
}
}
然而,当我尝试将其拉入android以在临时值过高时发出通知时,应用程序就会崩溃。
Android代码:
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notification(View view) throws Exception
{
addNotification();
}
private void addNotification() throws Exception
{
String fridge = "All fridges are normal";
fridge = getFridge();
String temp = "All temperatures are normal";
temp = getTemp();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this,"notify_001")
.setSmallIcon(R.drawable.message)
.setContentTitle(fridge) //this is the title of notification
.setColor(101)
.setContentText(temp); //this is the message showed in notification
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
public static String getText(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"UTF8"));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
public static JSONObject parser() throws Exception{
JSONObject json = new JSONObject(getText(WEB URL));
return json;
}
public static String getFridge() throws Exception{
String thisFridge = "No issues.";
JSONObject json = parser();
JSONArray n = json.getJSONArray("fridgedetail");
for (int i=0; i<n.length(); i++){
JSONObject m = n.getJSONObject(i);
if(m.getDouble("TempF")>40.00)
thisFridge = m.getString("name");
}
return thisFridge;
}
public static String getTemp() throws Exception{
String tempIs = "Temperature is normal";
return tempIs;
}
}