Java Android应用程序无法将详细信息发布到url
我有一个发布到“”的Android应用程序。一旦被访问,应用程序就会向脚本发布一个id密钥,php文件将其保存到我的文件服务器上的一个文本文件中。 我已经在我的浏览器中手动输入了url,它成功地生成了一个空文本文件,因为没有收到任何帖子数据,但当我在我的应用程序上测试它时,它不起作用 我使用Asynchttp像这样调用和传递数据Java Android应用程序无法将详细信息发布到url,java,php,android,restful-url,Java,Php,Android,Restful Url,我有一个发布到“”的Android应用程序。一旦被访问,应用程序就会向脚本发布一个id密钥,php文件将其保存到我的文件服务器上的一个文本文件中。 我已经在我的浏览器中手动输入了url,它成功地生成了一个空文本文件,因为没有收到任何帖子数据,但当我在我的应用程序上测试它时,它不起作用 我使用Asynchttp像这样调用和传递数据 private void backgroundRegister(final String username){ new AsyncTask<Void, V
private void backgroundRegister(final String username){
new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
String msg = "";
try{
if(gcmObj==null){
gcmObj = GoogleCloudMessaging.getInstance(getApplicationContext());
}
registerID = gcmObj.register(AppConstants.PROJ_ID);
msg = "Registration ID : " + registerID;
Log.d("REGOID",registerID);
}catch(IOException e){
Log.d("IOEXCEPTION",e.toString());
}
return msg;
}
@Override
protected void onPostExecute(String s) {
if(!TextUtils.isEmpty(registerID)){
savetoSharedPrefs(getApplicationContext(),registerID,username);
Toast.makeText(getApplicationContext(),"Registered with GCM",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Registration failed",Toast.LENGTH_LONG).show();
}
}
}.execute(null, null, null);
}
private void savetoSharedPrefs(Context context, String registerID,String userName){
SharedPreferences prefs = getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(regID, registerID);
editor.putString(userNAME, userName);
editor.commit();
storeREG();
}
//store in the file server (PHP)
private void storeREG(){
pg.show();
params.put("regID", registerID);
//Make RESTful webservice call
AsyncHttpClient client = new AsyncHttpClient();
client.post(AppConstants.SERVER_URL,params,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
pg.hide();
if(pg!=null){
pg.dismiss();
}
Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
Intent home = new Intent(getApplicationContext(),HomeActivity.class);
home.putExtra("regID",registerID);
Log.d("REGID",registerID);
startActivity(home);
finish();
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
pg.hide();
if(pg!=null){
pg.dismiss();
}
if(statusCode==404){
Toast.makeText(getApplicationContext(),"Requested resource not found",Toast.LENGTH_LONG).show();
}else if(statusCode==500){
Toast.makeText(getApplicationContext(),"Something went wrong at the server",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Unexpected error occurred",Toast.LENGTH_LONG).show();
}
}
});
}
我的舱单以防万一
<?xml version="1.0" encoding="utf-8"?>
更新:
我已经设法使用我的mamp服务器在本地运行了这段代码,当我将它上传到这个主机时,问题仍然存在 为了使用
asyncandoridhttp
库传递Post参数,您可以使用提供的RequestParams
类,并将其传递给Post
API调用,我的意思是:
AsyncHttpClient client = new AsyncHttpClient();
RequestParams req = new RequestParams();
req.add("shareRegId","true");
client.post(AppConstants.SERVER_URL,req,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
pg.hide();
if(pg!=null){
pg.dismiss();
}
Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
Intent home = new Intent(getApplicationContext(),HomeActivity.class);
home.putExtra("regID",registerID);
Log.d("REGID",registerID);
startActivity(home);
finish();
}
这将帮助您传递
Post-Params
,为了使用asyncandoridhttp
库传递Post-Params,您可以使用提供的RequestParams
类并将其传递到Post
API调用,这是我的一点意思:
AsyncHttpClient client = new AsyncHttpClient();
RequestParams req = new RequestParams();
req.add("shareRegId","true");
client.post(AppConstants.SERVER_URL,req,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
pg.hide();
if(pg!=null){
pg.dismiss();
}
Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
Intent home = new Intent(getApplicationContext(),HomeActivity.class);
home.putExtra("regID",registerID);
Log.d("REGID",registerID);
startActivity(home);
finish();
}
这将帮助您传递
Post参数
,如何在此处传递参数?通过附加到url?通过附加到url和通过post向url传递数据?我想这就是问题所在你好,谢谢你的回复。我编辑了问题中的代码。我通过URL旁边的参数传递数据,但我仍然无法解决此问题。使用Postman或Restful测试客户机进行的任何形式的测试返回的状态代码都是200,表示OK?通过附加到url?通过附加到url和通过post向url传递数据?我想这就是问题所在你好,谢谢你的回复。我编辑了问题中的代码。我通过URL旁边的参数传递数据,但我仍然无法解决此问题。使用Postman或Restful测试客户端进行的任何形式的测试都会返回一个200状态代码,表示OK。我已经添加了我的php脚本。它应该使用GET检查shareRegId是否为空,然后从POST获取数据。到目前为止,它似乎根本没有联系该网站。问题是,我不知道client.post是否能够发送除了在php端创建的文本文件之外的任何信息。目前,我的所有权限都是正确的,但暂时没有生成任何文本文件。请将您的post
更改为get
,并在php脚本中使用$\u get
来获取这两个参数,并查看发生了什么我更改了它并尝试使用get。但是,文件服务器似乎没有从我的应用程序接收任何连接。同样的结果。什么也没发生。我的舱单已设置为允许使用internet。但是,当我复制该url并将其粘贴到浏览器上时,可以创建该文件。重写onFailure
method ofAsyncHttpResponseHandler
并查看是否返回错误我添加了php脚本。它应该使用GET检查shareRegId是否为空,然后从POST获取数据。到目前为止,它似乎根本没有联系该网站。问题是,我不知道client.post是否能够发送除了在php端创建的文本文件之外的任何信息。目前,我的所有权限都是正确的,但暂时没有生成任何文本文件。请将您的post
更改为get
,并在php脚本中使用$\u get
来获取这两个参数,并查看发生了什么我更改了它并尝试使用get。但是,文件服务器似乎没有从我的应用程序接收任何连接。同样的结果。什么也没发生。我的舱单已设置为允许使用internet。但是,当我复制该url并将其粘贴到浏览器上时,可以创建该文件。覆盖onFailure
方法的AsyncHttpResponseHandler
,并查看是否返回错误
AsyncHttpClient client = new AsyncHttpClient();
RequestParams req = new RequestParams();
req.add("shareRegId","true");
client.post(AppConstants.SERVER_URL,req,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
pg.hide();
if(pg!=null){
pg.dismiss();
}
Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
Intent home = new Intent(getApplicationContext(),HomeActivity.class);
home.putExtra("regID",registerID);
Log.d("REGID",registerID);
startActivity(home);
finish();
}