0

我的 Android 登录应用程序出现问题。该应用程序将获取用户名和密码并单击按钮登录,该数据将发送到我的 Wamp 服务器中的 URL,以使用 PHP 搜索用户的名称和密码,然后将数据编码为 JSON。

如果用户在数据库中,则返回 JSON 对象中值为 1 的成功属性,或者如果用户不在数据库中,则返回成功值为 0。我在 namevaluepair 参数下放置了一个 logcat,用于发布用户名和密码。

但是,当我在 AVD 中运行应用程序时,我返回 Toast 说连接错误,当我检查 logcat 时,我从我在名称值对下设置的名称值对中收到一个错误,说未发送查询。我不知道我做错了什么。

这是代码

JsonParser.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    private InputStream is = null;
    private JSONObject jObj = null;
    private String json;

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            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();

        } catch (UnsupportedEncodingException e) {
            Log.e("unsupported encoding", e.toString());

        } catch (ClientProtocolException e) {
            //e.printStackTrace();
            Log.e("client protocol error", e.toString());
        } catch (IOException e) {
            //e.printStackTrace();
            Log.e("IO error", e.toString());
        }

        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");
            }
            json = sb.toString();
              is.close();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;
    }
}

登录活动.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

    private EditText user_name;
    private EditText user_password;
    private Button   login_button;

    // URL to make request
    private static final String URL = "http://10.0.2.2/HMS2/services.php";
    private static final String SUCCESS = "success";

    JSONParser jParser = new JSONParser();
    JSONObject json = new JSONObject();

    //String object that will store the user input from the edit text widget
    String user;
    String pass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        user_name = (EditText) findViewById(R.id.username);
        user_password = (EditText) findViewById(R.id.userpassword);
        login_button = (Button)findViewById(R.id.login_button);

        //waits for the button to be clicked 
        login_button.setOnClickListener(this);
    }

    public void onClick(View v) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);

        //gets the user's name from edit text widget and stores it in a variable
        user = user_name.getText().toString();
        user.trim();

        //gets the user's name from the edit text widget and stores it in a variable
        pass = user_password.getText().toString();
        pass.trim();

        params.add(new BasicNameValuePair("username", user));
        params.add(new BasicNameValuePair("password", pass));
        Log.e("error", "did not get the query");

        //return a JSON object
        json = jParser.getJSONFromUrl(URL, params);
        Log.e("error", "did not get back json");

        try {   
            //returns a the key value
            int success = json.getInt(SUCCESS);

            if(success == 1) {
                Toast.makeText(this, " Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Wrong username or password", Toast.LENGTH_LONG).show();
            }
        } catch(Exception e) {
            Toast.makeText(this, "Connection problem", Toast.LENGTH_LONG).show();
        }
    }
}
4

1 回答 1

0

代码的结构方式每次运行时都会看到这些消息。无论错误情况如何。如果您想测试错误,请检查“getJSONFromUrl”的返回值或将该行放在 try/catch 中。

消息“字符 0 处的输入结束”听起来像是服务器正在向您发送一个空字符串。您可以发布该代码吗?

于 2012-10-18T12:22:04.720 回答