0

私はアンドロイド開発に不慣れで、パスワードとユーザー名をphpスクリプトにjson配列として送信するログインページを作成しようとしています.phpスクリプトは、それに応じてメッセージを含むjson配列応答を返します。

私はアンドロイドコードを次のように作成しました:

            jobj.put("uname", userName);
            jobj.put("password", passWord);
            JSONObject re = JSONParser.doPost(url, jobj);
            Log.v("Received","Response received . . ."+re);
            // Check your log cat for JSON reponse
            Log.v("Response: ", re.toString());
            int success = re.getInt("success");
            if (success == 1) {
                return 1;
            }
            else{
                return 0;
            }
        }
        catch(Exception e){ e.getMessage(); }
    }

JsonParser doPost コードは次のとおりです。

public static JSONObject doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        HttpEntity entity;
        StringEntity s = new StringEntity(c.toString());

        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        entity = s;
        request.setEntity(entity);
        Log.v("entity",""+entity);
        HttpResponse response;
        try{
            response = httpclient.execute(request);
            Log.v("REceiving","Received . . .");
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();
            Log.v("RESPONSE",""+is);
        }
        catch(Exception e){ 
            Log.v("Error in response",""+e.getMessage());
            }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            Log.v("Reader",""+reader.readLine());
            while ((line = reader.readLine()) != null) {
                 Log.v("line",""+line);
                sb.append(line + "\n");
            }
            Log.v("builder",""+sb);
            is.close();
            json = sb.toString();

        } catch (Exception e) {
            Log.v("Buffer Error", "Error converting result " + e.toString());
        }

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

        // return JSON String
        return jObj;
    }

}

私はphpスクリプトを次のように持っています:

$response = array();
$con=mysqli_connect("localhost","uname","password","db_manage");
if((isset($_POST['uname']) && isset($_POST['password']))){
$empid = $_POST['uname'];

$pass = $_POST['password'];
$query = "SELECT mm_emp_id,mm_password FROM employee_master WHERE mm_emp_id='$empid'and mm_password='$pass'";
$result = mysqli_query($con, $query);
if(count($result) > 0){
    $response["success"] = 1;
    $response["message"] = "";
    echo json_encode($response);
}
else{
    $response["success"] = 0;
    $response["message"] = "The username/password does not match";
    echo json_encode($response);
}
}

isset() をチェックする行で未定義のインデックスを取得しています。PHPスクリプトでjsonを受信する際に何が間違っていますか?

私がリンクを使用したことがわかる場合は、助けてください。

4

2 に答える 2

0

doPost メソッドでは、変数を含む JSON オブジェクト (JSONobject c) を使用しません。

于 2014-01-05T18:19:37.823 に答える