在android应用程序中与服务器通信并识别用户
我对编程基本上是新手,目前正在尝试在android上构建我的第一个移动应用程序。我正在考虑创建一个应用程序,它将向服务器发送请求,然后服务器将处理请求并在应用程序中加载数据,然后用户将查看这些数据 我有以下问题: 与服务器通信 我将如何编程应用程序,使其能够与服务器通信(我计划使用godaddy/hostgator等web主机) 登录和识别 我将如何唯一地识别应用程序的每个用户?是否有可能为从市场下载我的应用程序并首次运行它的任何用户识别和创建一个帐户(可能通过他们用于android市场的gmail帐户?)或者我必须让每个用户在第一次运行我的应用程序时手动注册和登录 如果有人能告诉我开始开发这样一个应用程序的最佳方式和最佳方式,我将不胜感激在android应用程序中与服务器通信并识别用户,android,mobile,Android,Mobile,我对编程基本上是新手,目前正在尝试在android上构建我的第一个移动应用程序。我正在考虑创建一个应用程序,它将向服务器发送请求,然后服务器将处理请求并在应用程序中加载数据,然后用户将查看这些数据 我有以下问题: 与服务器通信 我将如何编程应用程序,使其能够与服务器通信(我计划使用godaddy/hostgator等web主机) 登录和识别 我将如何唯一地识别应用程序的每个用户?是否有可能为从市场下载我的应用程序并首次运行它的任何用户识别和创建一个帐户(可能通过他们用于android市场的gma
谢谢 您可以使用json向服务器发送数据一个向服务器发送数据以发送邮件的简单示例 服务器端(Php脚本):
客户端:
public static void sendData(String name, String to, String from, String subject, String message)
{
String content = "";
try
{
/* Sends data through a HTTP POST request */
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://your.website.com");
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("to", to));
params.add(new BasicNameValuePair("from", from));
params.add(new BasicNameValuePair("subject", subject));
params.add(new BasicNameValuePair("message", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* Reads the server response */
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = in.read()) != -1)
{
sb.append((char) chr);
}
content = sb.toString();
in.close();
/* If there is a response, display it */
if (!content.equals(""))
{
Log.i("HTTP Response", content);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
公共静态void sendData(字符串名称、字符串收件人、字符串发件人、字符串主题、字符串消息)
{
字符串内容=”;
尝试
{
/*通过HTTP POST请求发送数据*/
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://your.website.com");
List params=newarraylist();
参数添加(新的BasicNameValuePair(“名称”),名称);
参数添加(新的BasicNameValuePair(“to”,to));
参数添加(新的BasicNameValuePair(“from”,from));
参数添加(新的BasicNameValuePair(“主题”,主题));
添加(新的BasicNameValuePair(“消息”,消息));
setEntity(新的UrlEncodedFormEntity(params,HTTP.UTF_8));
/*读取服务器响应*/
HttpResponse response=httpClient.execute(httpPost);
InputStream in=response.getEntity().getContent();
StringBuffer sb=新的StringBuffer();
int-chr;
而((chr=in.read())!=-1)
{
sb.附加((char)chr);
}
content=sb.toString();
in.close();
/*如果有响应,请显示它*/
如果(!content.equals(“”)
{
Log.i(“HTTP响应”,内容);
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}
同样,您可以登录到login.php并将数据发送到服务器…
public static void sendData(String name, String to, String from, String subject, String message)
{
String content = "";
try
{
/* Sends data through a HTTP POST request */
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://your.website.com");
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("to", to));
params.add(new BasicNameValuePair("from", from));
params.add(new BasicNameValuePair("subject", subject));
params.add(new BasicNameValuePair("message", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* Reads the server response */
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = in.read()) != -1)
{
sb.append((char) chr);
}
content = sb.toString();
in.close();
/* If there is a response, display it */
if (!content.equals(""))
{
Log.i("HTTP Response", content);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}