0

Androidログインアプリに問題があります。アプリはユーザー名とパスワードを取得し、ボタンをクリックしてログインすると、そのデータがWampサーバーにあるURLに送信され、PHPを使用してユーザーの名前とパスワードを検索し、データをJSONにエンコードします。

ユーザーがデータベースに存在する場合、JSONオブジェクトで値が1の成功プロパティが返されます。データベースに存在しない場合、成功値0が返されます。ユーザー名とパスワードを投稿するnamevaluepairparamsの下にlogcatを配置します。

ただし、AVDでアプリを実行すると、接続エラーを示すToastが返され、logcatを確認すると、namevalueペアの下に設定したnamevaluepairから、クエリが送信されなかったというエラーが発生しました。何が間違っているのかわかりません。

これがコードです

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;
    }
}

LoginActivity.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 に答える