gpt4 book ai didi

java - 基本的 Android 应用方向

转载 作者:行者123 更新时间:2023-11-29 06:48:40 25 4
gpt4 key购买 nike

在使用 Eclipse 和 Java Android SDK 之前,我已经在各种编程类(class)中创建了基本的 Android 应用程序。我想创建的应用程序会要求用户输入稍后会被分析的信息。我希望人们能够将此数据与其他人的数据进行比较,因此我希望用户所做的每个条目都提交到数据库,以便稍后在有人尝试比较他们的数据时查询。我想要有关如何实现此目标的指导。我应该找到一个免费的托管站点并设置一个 Sql 服务器,还是有更好的方法来完成此操作?

编辑:只是为了好玩。

最佳答案

我是一个非常初级的 android 开发者,我发现使用像 mongolab.com 这样的云存储在线数据库。对用户提交的数据非常友好。数据库和服务器之间的通信必须通过 JSON 解析和 URI 请求来完成。

这里是您可以绑定(bind)到一个按钮的代码示例,该按钮将发送存储在字段 tempData 中的对象:

public void send(View view) {
String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA?apiKey="
+ apiKey;
try {

// make web service connection
final HttpPost request = new HttpPost(apiURI);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

// Build JSON string with GSON library
Gson gson = new Gson();

JsonElement jsonElement = gson.toJsonTree(tempData);
String json = gson.toJson(jsonElement);
StringEntity entity = new StringEntity(json);

Log.d("****Parameter Input****", "Testing:" + json);
request.setEntity(entity);
// Send request to WCF service
final DefaultHttpClient httpClient = new DefaultHttpClient();

new AsyncTask<Void, Void, Void>() {
@Override
public Void doInBackground(Void... arg) {
try {
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving: "
+ response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
// print status in log
String line = "";
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);

}
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
}.execute();

} catch (Exception e) {
e.printStackTrace();
}

}

下面是一个用于检索数据库中元素的代码示例:

public void load() {
String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA"
+ "?apiKey=" + apiKey;

Log.d("****Status Line***", "" + apiURI);

try {

// make web service connection
final StringBuilder builder = new StringBuilder();

final HttpGet request = new HttpGet(apiURI);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
final DefaultHttpClient httpClient = new DefaultHttpClient();

new AsyncTask<Void, Void, String>() {

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
doSomethingWithReceivedData(result); //THIS METHOD IS DEFINED IN BODY OF YOUR ACTIVITY
}

@Override
public String doInBackground(Void... arg) {
try {
HttpResponse response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {

HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();

BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Log.d("****Status Line***", "Success");

return builder.toString();

} else {
Log.d("****Status Line***",
"Failed to download file");
}

} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
}.execute();

} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 基本的 Android 应用方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com