将数据从android应用程序插入本地服务器
我已经开发了一个使用GCM通知的应用程序,我已经成功地向GCM注册了设备,并检索了regId,现在我想将此ID发送到我的服务器 我正在使用此代码发送注册表:将数据从android应用程序插入本地服务器,android,Android,我已经开发了一个使用GCM通知的应用程序,我已经成功地向GCM注册了设备,并检索了regId,现在我想将此ID发送到我的服务器 我正在使用此代码发送注册表: package com.example.gcm; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.ne
package com.example.gcm;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import android.content.Context;
import android.util.Log;
public class ShareExternalServer {
public String shareRegIdWithAppServer(final Context context,
final String regId) {
String result = "";
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("regId", regId);
try {
URL serverUrl = null;
try {
serverUrl = new URL(Config.APP_SERVER_URL);
} catch (MalformedURLException e) {
Log.e("AppUtil", "URL Connection Error: "
+ Config.APP_SERVER_URL, e);
result = "Invalid URL: " + Config.APP_SERVER_URL;
}
StringBuilder postBody = new StringBuilder();
Iterator<Entry<String, String>> iterator = paramsMap.entrySet()
.iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
postBody.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
postBody.append('&');
}
}
String body = postBody.toString();
byte[] bytes = body.getBytes();
HttpURLConnection httpCon = null;
try {
httpCon = (HttpURLConnection) serverUrl.openConnection();
httpCon.setDoOutput(true);
httpCon.setUseCaches(false);
httpCon.setFixedLengthStreamingMode(bytes.length);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = httpCon.getOutputStream();
out.write(bytes);
out.close();
int status = httpCon.getResponseCode();
if (status == 200) {
result = "RegId shared with Application Server. RegId: "
+ regId;
} else {
result = "Post new one1 Failure." + " Status: " + regId;
}
} finally {
if (httpCon != null) {
httpCon.disconnect();
}
}
} catch (IOException e) {
result = "Post Failure 33 . Error in sharing with App Server."+regId;
Log.e("AppUtil", "Error in sharing with App Server: " + e);
}
return result;
}
}
package com.example.gcm;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Map.Entry;
导入android.content.Context;
导入android.util.Log;
公共类ShareExternalServer{
公共字符串shareRegIdWithAppServer(最终上下文,
最终字符串(regId){
字符串结果=”;
Map paramsMap=newhashmap();
参数映射put(“regId”,regId);
试一试{
URL服务器URL=null;
试一试{
serverUrl=新URL(Config.APP\u SERVER\u URL);
}捕获(格式错误){
Log.e(“AppUtil”,“URL连接错误:”
+Config.APP_SERVER_URL,e);
结果=“无效URL:”+Config.APP\u服务器\u URL;
}
StringBuilder postBody=新建StringBuilder();
迭代器迭代器=paramsMap.entrySet()
.iterator();
while(iterator.hasNext()){
Entry param=iterator.next();
postBody.append(param.getKey()).append('='))
.append(param.getValue());
if(iterator.hasNext()){
postBody.append('&');
}
}
字符串体=postBody.toString();
byte[]bytes=body.getBytes();
HttpURLConnection httpCon=null;
试一试{
httpCon=(HttpURLConnection)serverUrl.openConnection();
httpCon.setDoOutput(真);
httpCon.setUseCaches(假);
httpCon.setFixedLengthStreamingMode(字节.长度);
httpCon.setRequestMethod(“POST”);
httpCon.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=UTF-8”);
OutputStream out=httpCon.getOutputStream();
out.write(字节);
out.close();
int status=httpCon.getResponseCode();
如果(状态==200){
result=“与应用程序服务器共享的RegId。RegId:”
+雷吉德;
}否则{
结果=“发布新one1失败。”+”状态:“+regId;
}
}最后{
如果(httpCon!=null){
httpCon.disconnect();
}
}
}捕获(IOE异常){
result=“Post Failure 33.与应用程序服务器共享时出错。”+regId;
Log.e(“AppUtil”,“与应用服务器共享时出错:”+e);
}
返回结果;
}
}
您在服务器端使用的是哪种语言?服务器我使用的是java@Alok gupta请更新问题以解释您面临的问题。我需要将regId插入我的本地server@yazanyes我知道了,但是上面的代码怎么了?它是否到达服务器?它是否达到但服务器未保存值?它是否显示异常?你需要知道到底是什么问题