用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5154|回复: 12
收起左侧

[HELP] 用CI作为android的server

[复制链接]
发表于 2013-7-2 00:59:07 | 显示全部楼层 |阅读模式
我做了一个手机端,实现简单的登陆注册,采用通过json通信,参照http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ 不过我用CI作为server端。可是在执行httpClient.execute(httpPost);总是报一堆错误,不用CI作为server就正常~ 请问有人了解这是什么原因吗?是不是ci的url问题?
发表于 2013-7-2 08:56:28 | 显示全部楼层
能把问题 贴出来么,这么说有个毛线用,再者这些问题跟框架有什么关系。。。。。。。。
发表于 2013-7-2 09:42:41 | 显示全部楼层
先用get測看看吧!
一定可以.
 楼主| 发表于 2013-7-2 14:27:05 | 显示全部楼层
本帖最后由 chengh06 于 2013-7-2 14:43 编辑
yuvista 发表于 2013-7-2 08:56
能把问题 贴出来么,这么说有个毛线用,再者这些问题跟框架有什么关系。。。。。。。。 ...

java部分代码,运行到HttpResponse httpResponse = httpClient.execute(httpPost);就出错了
JS复制代码
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.apache.http.protocol.HTTP;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
 
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
 
public class MainActivity extends Activity {
       
        Button btnLoginButton;
        EditText inputEmail;
        EditText inputPassword;
        TextView loginErrorMsg;
       
        private static String KEY_SUCCESS = "success";
        private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED = "created";
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
               
                inputEmail = (EditText) findViewById(R.id.loginEmail);
                inputPassword = (EditText) findViewById(R.id.loginPassword);
                btnLoginButton = (Button) findViewById(R.id.btnLogin);
                loginErrorMsg = (TextView) findViewById(R.id.login_error);
               
                btnLoginButton.setOnClickListener(new View.OnClickListener() {
                       
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                String email = inputEmail.getText().toString();
                                String password = inputPassword.getText().toString();
                                InputStream is = null;
                                String j = null;
                                JSONObject jObj = null;
                               
                                List<NameValuePair> params = new ArrayList<NameValuePair>();
                                params.add(new BasicNameValuePair("email", email));
                                params.add(new BasicNameValuePair("password", password));
                               
                                DefaultHttpClient httpClient = new DefaultHttpClient();
                               
                                try {
                                        HttpPost httpPost = new HttpPost("http://www.zhaoxi.me/auth/login/app_login");
                                        httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
                                        HttpResponse httpResponse = httpClient.execute(httpPost);
                                        HttpEntity httpEntity = httpResponse.getEntity();
                                        is = httpEntity.getContent();
                                       
                                } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                               
                                try {
                                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
                                        StringBuffer sb = new StringBuffer();
                                        String line = null;
                                        while ((line = reader.readLine()) != null) {
                                sb.append(line + "n");
                            }
                                        is.close();
                                        j = sb.toString();
                                       
                                } catch (Exception e) {
                                        // TODO: handle exception
                                        e.printStackTrace();
                                }
                               
                                try {
                                        jObj = new JSONObject(j);  
                                } catch (Exception e) {
                                        // TODO: handle exception
                                        e.printStackTrace();
                                }
                               
                                try {
                                        if(jObj.getString(KEY_SUCCESS)!=null){
                                                String res = jObj.getString(KEY_SUCCESS);
                                                if(Integer.parseInt(res) == 1){
                                                        JSONObject json_user = jObj.getJSONObject("user");
                                                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
 
                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);
 
                            // Close Login Screen
                            finish();
                                                       
                                                }
                                               
                                        }
                                       
                                       
                                } catch (Exception e) {
                                        // TODO: handle exception
                                        e.printStackTrace();
                                }
                               
                               
                        }
                });
               
        }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }
 
}
 
复制代码


php部分代码
PHP复制代码
 
public function app_login(){
        $email = $this->input->post('email',TRUE);
        $password = $this->input->post('password',TRUE);
 
        if(!$this->auth->check_user($email)){
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
            return;
        }else{
            log_message("info","app_login");
            user = getuser()//省略              
                if(!empty($user)){
                    $this->auth->process_login($user);
                    $response["success"] = 1;
                    $response["user"]["name"] = $user["name"];
                    $response["user"]["email"] = $user["email"];
                    $response["user"]["created"] = $user["created"];
                    echo json_encode($response);
                    return;
                }else{
                    $response["error"] = 1;
                    $response["error_msg"] = "Incorrect email or password!";
                    echo json_encode($response);
                    return;
                }
 
        }
 
        }
 
复制代码

发表于 2013-7-2 14:32:11 | 显示全部楼层
你有没有做rewrite?
没有的话就要带上index.php
http://www.zhaoxi.me/auth/regist/app_login这个打开就是not found
 楼主| 发表于 2013-7-2 14:36:51 | 显示全部楼层
gogogo1027 发表于 2013-7-2 14:32
你有没有做rewrite?
没有的话就要带上index.php
http://www.zhaoxi.me/auth/regist/app_login这个打开就是 ...

rewrite了~
发表于 2013-7-2 14:38:26 | 显示全部楼层
加Q349703434吧
http://www.zhaoxi.me/auth/regist/app_login
这个应该要做路由吧,默认的controller是神马
 楼主| 发表于 2013-7-2 14:48:04 | 显示全部楼层
gogogo1027 发表于 2013-7-2 14:38
加Q349703434吧
http://www.zhaoxi.me/auth/regist/app_login
这个应该要做路由吧,默认的controller是神马 ...

不好意思,刚才代码贴错了,是login/app_login
不过这个问题不影响,原来在regist路径下也是不行~
发表于 2013-7-2 15:02:43 | 显示全部楼层
你先检查一下你返回的json数据是什么。
发表于 2013-7-4 14:16:12 | 显示全部楼层
查看一下url正確性,
還有檢查一下環境設定,
比如檢查能不能跨網域請求有沒有關閉.

在android端請輸出所有錯誤碼,
這樣可以更清礎錯誤的原因.

本版积分规则