- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序可以将姓名和密码从 Android 发送到 PHP。
我的安卓代码:
package com.example.com.tourism;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SignUp extends Activity{
EditText first,last,birth ,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
Button signUp=(Button)findViewById(R.id.sign_up);
first =(EditText) findViewById(R.id.edfname);
last =(EditText) findViewById(R.id.edlname);
pass =(EditText) findViewById(R.id.edpass);
signUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String firstn = first.getText().toString();
String lastn = last.getText().toString();
String passw = pass.getText().toString();
try {
JSONObject json = new JSONObject();
json.put("first_name", firstn);
json.put("last_name", lastn);
json.put("password", passw);
postData(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://10.0.0.2:3784/tourism/index.php/site/register");
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
//httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
}
}catch (Exception e) {
e.printStackTrace();`enter code here`
}
}
}
在php codeigniter Controller 中接收前置信息的函数是
function register_get()
{
$json = array('status' => false );
if($this->input->post()==null){
$this -> response($json, 200);
}
$firstname = $this->post("first_name");
$lastname = $this->post("last_name");
$password = $this->post("password");
if(!$firstname || !$lastname || !$password){
$json['status'] = "wrong insert";
$this -> response($json, 200);
}
$this->load->model('Data_model');
$result = $this->Data_model->search($firstname, $lastname);
if($result)
{
$this->Data_model->insert($firstname,$lastname,$password);
$json['status'] = true;
}
// here if false..
$this -> response($json, 200);
}
我尝试了 PHP 代码并且它有效我认为问题来自 android 但我不知道它在哪里,任何人都可以帮助我吗???
最佳答案
您已经创建了 WebService,它在 POST 变量中接受请求并以 JSON 格式给出响应。
我给你举个例子。
CodeIgniter 中 Controller 的完整代码:
class info_control extends CI_Controller {
//consrtuctor
public function __construct()
{
parent::__construct();
/*
load you helper library
*/
/*
load you model
*/
$this->load->library('form_validation');
}
function register_get()
{
/* What you have done that i don't know */
/*
$json = array('status' => false );
if($this->input->post()==null){
$this -> response($json, 200);
}
*/
/* This is Form Validation */
$this->form_validation->set_rules('first_name', 'FirstName', 'valid_email|required');
$this->form_validation->set_rules('last_name', 'LastName', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
/* Here you mistake $this->post('post var')*/
$firstname = $this->input->post("first_name");
$lastname = $this->input->post("last_name");
$password = $this->input->post("password");
/** YOUR PROCESS for REGISTER **/
/*
LIKE
*/
if ($this->form_validation->run() == TRUE)
{
if(/* REGISTRATION SUCCESSFUL */)
{
$data = array('result' => 'success', 'msg' => 'Registered Successfully...');
}
else
{
$data = array('result' => 'error', 'msg' => 'Email Already Used or Invalid - Unable to Create Account' );
}
}
else
{
$data = array('result' => 'error','msg' => preg_replace("/[\n\r]/",".",strip_tags(validation_errors())));
}
echo json_encode($data);
/*
if(!$firstname || !$lastname || !$password){
$json['status'] = "wrong insert";
$this -> response($json, 200);
}
$this->load->model('Data_model');
$result = $this->Data_model->search($firstname, $lastname);
if($result)
{
$this->Data_model->insert($firstname,$lastname,$password);
$json['status'] = true;
}
// here if false..
$this -> response($json, 200);
*/
}
现在,在 Android 中,您必须发送如下数据:
static InputStream is;
static String result;
static JSONObject jsonObject;
static HttpClient httpClient;
static HttpPost httpPost;
static List<NameValuePair> pairs;
static HttpResponse response;
static HttpEntity entity;
private static void init() {
// TODO Auto-generated constructor stub
is = null;
result = "";
jsonObject = null;
httpPost = null;
httpClient = new DefaultHttpClient();
}
private static String getResult(InputStream is) {
// TODO Auto-generated method stub
BufferedReader reader;
StringBuilder stringBuilder = null;
try {
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stringBuilder.toString();
}
//for Register Employee
public static JSONObject registerUser(String url, String firstNameString, String lastNameString, String passwordString) {
// TODO Auto-generated method stub
try{
init();
Log.d("msg", "URL : "+url);
httpPost = new HttpPost(url.toString());
Log.d("msg", "post : "+httpPost);
pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("firstname", firstNameString));
pairs.add(new BasicNameValuePair("lastname", lastNameString));
pairs.add(new BasicNameValuePair("password", passwordString));
httpPost.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));
Log.d("msg", "httppost : "+httpPost.toString());
response = httpClient.execute(httpPost);
Log.d("msg", "res : "+response.getStatusLine().getStatusCode());
entity = response.getEntity();
is = entity.getContent();
Log.d("msg", ""+is);
/*//** Convert response to string **/
result = getResult(is);
jsonObject = new JSONObject(result);
Log.d("msg", "jsonObj : "+jsonObject);
}
catch(ClientProtocolException e){
Log.e("log_tag", "Error in Client Protocol : "+e.toString());
}catch(JSONException e){
Log.e("log_tag", "Error Parsing data "+e.toString());
}catch(Exception e){
Log.e("log_tag", "Error in HTTP Connection : "+e.toString());
}
return jsonObject;
}
关于php - Android和PHP codeigniter连接的简单代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17587372/
我想扩展调用 getMessage 时返回自定义消息的异常类。 class MY_Exceptions extends CI_Exceptions{ function __construct
我已经安装了一个干净的 Apache2(加上 PHP 和 MySQL)服务器并启用了 mod_rewrite在 apache 配置中。 我添加了 .htaccess文件以从 url 中删除 index
我正在使用上传类上传图片。但是我上传的图片的存储位置是:http://www.mysite.com/uploads/ 此文件夹的绝对路径是:c:\wamp\www\mysite\uploads\ 应用
大家好 我想在codeigniter上下文中提供一些静态html页面。我不想绕过base_url中的index.php文件。 但是,当我使用对HTML文件的调用时,它不会显示404错误页面。 感谢已经
我一直想知道在模型中以 OO 风格编写代码的正确方法是什么。当然,您可以拥有一个从数据库中检索数据然后映射到模型级变量的函数。但是,当您在模型中有其他功能试图从 BD 获取其他数据时,这种方法会变得违
之前所有的 JOIN 尝试都给我留下了填充结果的 id、标题键的光盘或项目数据(可能发生了冲突)。 所以我有: item table fields: id, title disc table fiel
假设我在 Controller 中有一个名为 的方法 book($chapter,$page); 其中 $chapter 和 $page 必须是整数。要访问该方法,URI 将如下所示 book/cha
我有一个用户可以注册的页面。他在此过程中上传了个人资料照片。我想限制大小,但除了 $config['maxsize'] 之外,并没有太多强调 codeigniter 文档。我尝试了以下但我没有收到任何
我需要将 CodeIgniter 设置为真正的多语言网站。我已经搜索过,但找不到解决方案。 我已经测试了这种方法,但它不起作用。 ( http://codeigniter.com/wiki/Categ
CodeIgniter 中的常量是否可以用于整个站点中的重复文本(比如元标记和元描述)?就像是: define('METADESCRIPTION', 'This is my site'); 然后将 M
我已经在 CodeIgniter 的路由器中写了这个。 $route['companyname'] = "/profile/1"; 这工作正常,但是当我在 URL 中键入“公司名称”时,它不起作用。这
我正在开始我的第一个 CodeIgniter 项目,并希望在开始之前获得一些建议。我对 Controller 和模型的名称如何工作感到有些困惑。 如果我希望我公司页面的网址为 http://examp
可以在CodeIgniter Active Record中使用多个INSERT记录,而无需for,foreach等。 我当前的代码: foreach($tags as $tag) { $tag
SELECT * FROM certs WHERE id NOT IN (SELECT id_cer FROM revokace); 如何在 CodeIgniter 事件记录中编写上述 select
wkhtmltopdf 听起来是一个很好的解决方案...问题是 exec 上没有任何反应 shell_exec("c:\wkhtmltopdf.exe","http://www.google.com
我当前的CodeIgniter有点问题。我有一个带有“页面” Controller 的CI安装程序,该 Controller 可从/ views加载静态文件,但它最多只能包含1个子文件夹,而我正在向其
有一段时间,我一直在处理分页类中的一个问题。 问题是,除了第 1 页的链接之外,所有分页的内容都可以。 所有链接都是这样的: example.com/method/page/2 example.com
我想对请求进行一些预处理和后处理,例如处理身份验证、加载上下文数据、性能时间等等。来自 Django 的概念是 MIDDLEWARE_CLASSES这让我可以在各个阶段处理请求:https://doc
我想通过创建自己的库和配置文件在 CodeIgniter 中生成全局变量。这就是我在我的库文件中编写的,比如说 globalvars.php。我把它放在/application/libraries 中
我有以下分页样式 Previous Page
我是一名优秀的程序员,十分优秀!