我正在为我的项目构建一个移动应用程序,必须在该应用程序上检索发送到Thingspeak的数据。
这是移动应用界面:https://i.stack.imgur.com/70erW.png 当我点击一个图像按钮时,它应该打开一个新的活动,它将显示从Thingspeak检索到的最后一个值。
我遇到的问题是,当我单击图像按钮时,该应用程序将停止运行,该按钮应该会将我带入另一个活动。它确实打开了一个新活动,表明导航到另一个活动正在工作,但是随后显示“不幸的是,智能农业已停止工作”。我认为问题xml出在活动文件中,或者是TextView我在某些TempHumidity.java文件中使用过的文件,但我真的不知道如何解决这些问题,因为他们是Android Studio中的绝对初学者。我有5个文件分别是activity_main.xml,MainActivity.java,TempHumidity.java,AndroidManifest.xml,imgbtnmenu.xml
TempHumidity.java 类(包含HttpURLConnection代码)
public class TempHumidity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imgbtnmenu); new GetMethodDemo().execute("https://thingspeak.com/channels/357670/field/1/last"); } public class GetMethodDemo extends AsyncTask<String , Void ,String> { String server_response; private TextView aTextView; @Override protected String doInBackground(String... strings) { URL url; HttpURLConnection urlConnection = null; try { url = new URL(strings[0]); urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK){ server_response = readStream(urlConnection.getInputStream()); Log.v("CatalogClient", server_response); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.e("Response", "" + server_response); aTextView.setText(s); } } // Converting InputStream to String private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer response = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { response.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return response.toString(); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.teerna.smartagriculture"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TempHumidity"> // you need to add these 3 lines inside application tag. </activity> </application> </manifest>
imgbtnmenu.xml 用于新的活动菜单(点击“湿度和温度图像”按钮时打开的菜单)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout>
Logcat上的消息如下: 在此处输入图片说明https://i.stack.imgur.com/Ws6r6.png
在此处输入图片说明https://i.stack.imgur.com/vqadV.png
在此处输入图片说明https://i.stack.imgur.com/bKrVj.png
在此处输入图片说明https://i.stack.imgur.com/uh5so.png