使用(提交和请求)具有 Android 可穿戴设备的数据库
Using (Submission and request) a database with a Android wearable
我想在按下 可穿戴应用 上的按钮时将值写入服务器上的数据库。我在 ("normal") Android 应用程序(下面的代码)上试用了该应用程序,它运行良好。但是一旦我想将它复制到可穿戴应用程序,就没有任何效果。我在可穿戴应用程序上所做的唯一不同是在清单中
<uses-feature android:name="android.hardware.type.watch" />
这是适用于手持设备但不适用于穿戴式设备的应用程序代码。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.demo.firstdatabaseapp" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
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>
</application>
</manifest>
主要活动:
package de.demo.firstdatabaseapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button bSubmit;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
bSubmit = (Button) findViewById(R.id.buttonSubmit);
// This where the Values get entered inside the database
bSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View arg0) {
// Storing the values inside the editTexts inside the string variables
String name = "Chuck";
String age = "42";
String email = "Norris@awesomeness.com";
//Setting the nameValuePairs
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//Adding the string variabls inside the nameValuePairs
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("age", age));
nameValuePairs.add(new BasicNameValuePair("email", email));
// Setting up the connection inside the try catch block
try{
//Setting up the default http client
HttpClient httpClient = new DefaultHttpClient();
//Setting up the http post method and passing the url in case
// of online database and the ip address in case of localhost database.
//And the php file which serves as the link between the android app
// and the database
HttpPost httpPost = new HttpPost("http://---.---.-.--:8888/tutorial.php");
//Passing the nameValuePairs inside the httpPost
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Getting the response
HttpResponse response = httpClient.execute(httpPost);
//Setting up the entity
HttpEntity entity = response.getEntity();
//Setting up the content inside an input stream reader
is = entity.getContent();
//Displaying a toast if the data is entered successfully
String msg = "Data entered successfully";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
//Writing the catch blocks to handle the exceptions
catch (ClientProtocolException e)
{
Log.e("ClientProtocol", "Log_tag");
e.printStackTrace();
}
catch (IOException e)
{
Log.e("Log_tag", "IOException");
e.printStackTrace();
}
}
});
}
}
布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
大多数可穿戴设备目前不支持直接互联网连接,因此尝试 upload/download 从 Wear 应用程序进行任何操作都行不通。
您应该使用 Data Layer API 将要保存的数据发送到手持设备上的配套应用程序,然后该应用程序可以进行上传并让 Wear 应用程序知道操作完成后发生的结果完成。
我可以使用 Teleport 库 (github.com/Mariuxtheone/Teleport) 解决我的问题,因此可以将磨损的数据通过 phone 发送到数据库。
我的 wear (Samsung Gear Live) 没有t support a direct internet connection. In my case I need to send it first via Bluetooth to the handheld. That
造成在手持设备和 wear 上使用代码的区别。
有关更多信息,我发现这个关于数据同步的演讲非常有趣 -> https://www.youtube.com/watch?v=jbbKCfCIyFA
我想在按下 可穿戴应用 上的按钮时将值写入服务器上的数据库。我在 ("normal") Android 应用程序(下面的代码)上试用了该应用程序,它运行良好。但是一旦我想将它复制到可穿戴应用程序,就没有任何效果。我在可穿戴应用程序上所做的唯一不同是在清单中
<uses-feature android:name="android.hardware.type.watch" />
这是适用于手持设备但不适用于穿戴式设备的应用程序代码。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.demo.firstdatabaseapp" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
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>
</application>
</manifest>
主要活动:
package de.demo.firstdatabaseapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button bSubmit;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
bSubmit = (Button) findViewById(R.id.buttonSubmit);
// This where the Values get entered inside the database
bSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View arg0) {
// Storing the values inside the editTexts inside the string variables
String name = "Chuck";
String age = "42";
String email = "Norris@awesomeness.com";
//Setting the nameValuePairs
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//Adding the string variabls inside the nameValuePairs
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("age", age));
nameValuePairs.add(new BasicNameValuePair("email", email));
// Setting up the connection inside the try catch block
try{
//Setting up the default http client
HttpClient httpClient = new DefaultHttpClient();
//Setting up the http post method and passing the url in case
// of online database and the ip address in case of localhost database.
//And the php file which serves as the link between the android app
// and the database
HttpPost httpPost = new HttpPost("http://---.---.-.--:8888/tutorial.php");
//Passing the nameValuePairs inside the httpPost
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Getting the response
HttpResponse response = httpClient.execute(httpPost);
//Setting up the entity
HttpEntity entity = response.getEntity();
//Setting up the content inside an input stream reader
is = entity.getContent();
//Displaying a toast if the data is entered successfully
String msg = "Data entered successfully";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
//Writing the catch blocks to handle the exceptions
catch (ClientProtocolException e)
{
Log.e("ClientProtocol", "Log_tag");
e.printStackTrace();
}
catch (IOException e)
{
Log.e("Log_tag", "IOException");
e.printStackTrace();
}
}
});
}
}
布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
大多数可穿戴设备目前不支持直接互联网连接,因此尝试 upload/download 从 Wear 应用程序进行任何操作都行不通。
您应该使用 Data Layer API 将要保存的数据发送到手持设备上的配套应用程序,然后该应用程序可以进行上传并让 Wear 应用程序知道操作完成后发生的结果完成。
我可以使用 Teleport 库 (github.com/Mariuxtheone/Teleport) 解决我的问题,因此可以将磨损的数据通过 phone 发送到数据库。
我的 wear (Samsung Gear Live) 没有t support a direct internet connection. In my case I need to send it first via Bluetooth to the handheld. That
造成在手持设备和 wear 上使用代码的区别。
有关更多信息,我发现这个关于数据同步的演讲非常有趣 -> https://www.youtube.com/watch?v=jbbKCfCIyFA