|
楼主 |
发表于 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;
}
}
}
复制代码
|
|