我正在尝试通过我的 android 应用程序进行用户注册,数据存储在使用 php 的数据库中。一切正常,即数据已插入数据库,但在 android 中看不到 JSON 响应。
这是我的代码
Java
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
// Add your data
try {
jsonParams.put("user_name", "Steve Jobs");
jsonParams.put("user_email", "steave@apple.com");
jsonParams.put("user_password", "nopassword");
jsonParams.put("user_phone_number", "1234567890");
jsonParams.put("club_member_id", "24");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("POST", jsonParams.toString()));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (JSONException e) {
}
// HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost(url);
// ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
// nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjd"));
// nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
// nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
// nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
// Log.e("Params", String.valueOf(nameValuePairs.toString()));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// HttpResponse response = httpclient.execute(httppost);
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
Log.e("Data", String.valueOf(json));
PHP
$output=array();
$data = json_decode($_POST['POST'], true);
if(!empty($data)){
if(user_exists($data['user_email'])){
$result = db_conncet()->prepare("INSERT INTO `mir_users`(`name`, `password`, `email`, `phone_number`, `club_member_id`) VALUES (?,?,?,?,?)");
$data['club_member_id']=(isset($data['club_member_id']))?$data['club_member_id']:'NULL';
$temp=array(
$data['user_name'],
password_hash($data['user_password'],PASSWORD_BCRYPT),
$data['user_email'],
$data['user_phone_number'],
$data['club_member_id']
);
$result->execute($temp);
if($result->rowCount()){
$output=array(
'status'=>true,
'code'=>'103',
'message'=>'Registration success'
);
}else{
$output=array(
'status'=>false,
'code'=>'102',
'message'=>'Registration error'
);
}
}else{
$output=array(
'status'=>false,
'code'=>'102',
'message'=>'Email-ID Already taken'
);
}
echo json_encode($output);
}
PHP 脚本工作正常,数据已插入数据库,但我在我的应用程序中看不到 echo json_encode($output) 的输出。
Log.e("Data", String.valueOf(json)); 在日志中获取空值。
我已经检查了 php 错误日志,我会得到这个
PHP Notice: Undefined index: POST in /home/zama2n/public_html/miradmin/api/functions.php on line 21
我认为这是在我的应用程序中获得空 json 响应的原因。但是插入查询工作正常 有什么问题。请帮帮我?
最佳答案
您有一个 url 但使用了两次。
首先使用 HttpClient,您发布了一些数据,但没有对 HttpResponse response = httpclient.execute(httppost);
之后,您使用 json 解析器调用相同的 url。没有数据。
一切都没有意义。
如果你说你无法得到回应,你在说什么?
`
关于php - 在 android 中看不到来自 php 的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34673920/
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
我花了几天时间尝试安装ruby1.9.2并让它与gems一起工作:-/我最终放弃了我的MacOSX10.6机器,下面是我的Ubuntu机器上的当前状态。任何建议将不胜感激!#rubytest.rb:29:in`require':nosuchfiletoload--mongo(LoadError)from:29:in`require'fromtest.rb:1:in`'#cattest.rbrequire'mongo'db=Mongo::Connection.new.db("mydb")#gemwhichmongo/usr/local/rvm/gems/ruby-1.9.2-p0/g
我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│ ├──dbconfig.rb│ ├──functions.rb│ └──init.rb├──hidden│ └──Rakefile├──lib│ ├──admin.rb│ ├──api.rb│ ├──indexer.rb│ ├──init.rb│ └──magnet.rb├──models│ ├──init.rb│ ├──invite.rb│ ├─