转换result.java.lang.NullPointerException时出错
我只是不知道如何解决这个错误。它还显示了日志cat中的错误“error Parsing Data.org.json.JSONException:字符0处输入结束”。我没有android的基本功能。但我真的需要这个。有人能帮我吗 这是我的JSONParser.java转换result.java.lang.NullPointerException时出错,java,php,android,Java,Php,Android,我只是不知道如何解决这个错误。它还显示了日志cat中的错误“error Parsing Data.org.json.JSONException:字符0处输入结束”。我没有android的基本功能。但我真的需要这个。有人能帮我吗 这是我的JSONParser.java public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; pub
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser(){
}
public JSONObject getJSONFromUrl(final String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line=reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch(Exception e)
{
Log.e("Buffer Error", "Error Converting Result " + e.toString());
}
try
{
jObj = new JSONObject(json);
}
catch(JSONException e)
{
Log.e("JSON Parser", "Error Parsing Data " + e.toString());
}
return jObj;
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params){
try{
if(method.equals("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equals("GET")){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch(Exception e){
Log.e("Buffer Error.", "Error Converting Result." + e.toString());
}
try{
jObj = new JSONObject(json);
}catch(JSONException e){
Log.e("JSON Parser", "Error Parsing Data." + e.toString());
}
return jObj;
}
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
公共JSONParser(){
}
公共JSONObject getJSONFromUrl(最终字符串url)
{
尝试
{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
捕获(不支持的编码异常e)
{
e、 printStackTrace();
}
捕获(客户端协议例外e)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
尝试
{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}
捕获(例外e)
{
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
尝试
{
jObj=新的JSONObject(json);
}
捕获(JSONException e)
{
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jObj;
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、列表参数){
试一试{
if(方法等于(“POST”)){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(method.equals(“GET”)){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”。+e.toString());
}
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
e(“JSON解析器”,“解析数据时出错。”+e.toString());
}
返回jObj;
}
}
这是我的login.java
public class Login extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit;
private TextView mRegister;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://10.111.70.109/webservice/register.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
mSubmit = (Button)findViewById(R.id.btnLogin);
mRegister = (TextView)findViewById(R.id.link_to_register);
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
break;
case R.id.link_to_register:
Intent i = new Intent(getApplicationContext(), SignUp.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String>
{
boolean failure = false;
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Please Wait. Attempting Login..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String...args){
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("Request!", "Starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("Login Attempt..", json.toString());
success = json.getInt(TAG_SUCCESS);
if(success==1){
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, MainActivity.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
if(file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
公共类登录扩展活动实现OnClickListener{
私人编辑文本用户,通过;
私有按钮mSubmit;
私有文本视图注册器;
私人对话;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串登录\u URL=”http://10.111.70.109/webservice/register.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
user=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
mSubmit=(按钮)findviewbyd(R.id.btnLogin);
mRegister=(TextView)findViewById(R.id.link\u to\u寄存器);
mSubmit.setOnClickListener(这个);
mRegister.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btnLogin:
新建AttemptLogin().execute();
打破
案例R.id.link_至_寄存器:
Intent i=newintent(getApplicationContext(),SignUp.class);
星触觉(i);
打破
违约:
打破
}
}
类AttemptLogin扩展了AsyncTask
{
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
pDialog.setMessage(“请稍候。正在尝试登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
成功;
字符串username=user.getText().toString();
字符串密码=pass.getText().toString();
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
Log.d(“请求!”,“启动”);
JSONObject json=jsonParser.makeHttpRequest(登录URL,“POST”,参数);
Log.d(“登录尝试”,json.toString());
success=json.getInt(TAG_success);
如果(成功==1){
Log.d(“登录成功!”,j
<?php
require("config.inc.php");
if(!empty($_POST))
{
$query = "SELECT username, email, password FROM students WHERE username =:username";
$query_params = array(':username'=>$_POST['username']);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Error. Please Try Again.";
die(json_encode($response));
}
$validated_info = false;
$row = $stmt->fetch();
if($row)
{
if($_POST['password']===$row['password'])
{
$login_ok = true;
}
}
if($login_ok)
{
$response["success"] = 1;
$response["message"] = "Login Successful!";
die(json_encode($response));
}else{
$response["success"] = 0;
$response["message"] = "Sorry. Please Try Again.";
die(json_encode($response));
}
}else{
?>
put:
}else{
$response["success"] = 0;
$response["message"] = "Sorry. Please Try Again.";
die(json_encode($response));
}
?>