我在互联网上搜索了一段时间,无法让我的Android应用程序将注册ID和设备ID发送到服务器。我确信我错过了一些简单的东西,但有人能看到我做错了什么吗?代码被击中,Toast被显示,但服务器什么都不做,但如果我直接转到URL,它会做我想做的事。
if (!regId.equals("")) {
Toast.makeText(context, "The registration ID is: " + regId,
Toast.LENGTH_LONG).show();
Toast.makeText(context, "The device ID is: " + deviceId,
Toast.LENGTH_LONG).show();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://uni.britintel.co.uk/register.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("deviceId", deviceId));
nameValuePairs.add(new BasicNameValuePair("registrationId", regId));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse httpresponse = httpclient.execute(httppost);
BufferedReader rd = new BufferedReader(new InputStreamReader(
httpresponse.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
Toast.makeText(context, line,
Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} else {
Toast.makeText(context, "The registration ID doesn't exist",
Toast.LENGTH_LONG).show();
}
一旦我让缓冲阅读器工作,它就会被删除,因为它对我想要的东西毫无意义。
提前感谢您的帮助。
我终于设法让它工作起来了。
我向清单xml添加了以下权限,但不确定它为什么修复了它…
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
我的服务器也有一些问题,但我在添加权限后才得到响应。