0

我只是不知道如何解决这个错误。它还在日志 cat 中显示错误“解析 Data.org.json.JSONException:End of input at character 0 of”。我没有安卓和所有的基础知识。但我真的需要这个。有人可以帮帮我吗?

这是我的 JSONParser.java

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;

}
}

这是我的 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();
    }
}

}

这是我用于登录的 php 脚本:

<?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{
?>

有谁能够帮助我?我真的很感激。

4

1 回答 1

0

你没有关闭最后一个}else{

put:

 }else{  
 $response["success"] = 0;
    $response["message"] = "Sorry. Please Try Again.";
    die(json_encode($response));
}  
?>
于 2013-11-02T07:07:40.547 回答