2016-03-18 30 views
0

サーバーにユーザー名とパスワードを送信したいと思い、Volleyのサーバーから別のユーザーの情報を受け取りたいのですが?ログインフォームのサーバーからVolleyのデータを送受信する方法

私はstringrequestを送ってjsonarrayrequestを受け取りたいと思いますが、今はどうしますか?

これはあなたがJSONObjectは、ユーザ名とパスワードが含まれている送信し、JSONObjectはこのように、ユーザーまたはエラーmessage.Useバレーの情報が含まれて受信することができ、私のPHPコード

if($_SERVER['REQUEST_METHOD']=='POST'){ 
    //Getting values 
    $username = $_POST['email']; 
    $password = $_POST['password'];  
    header('Content-Type: application/json; charset=utf-8'); 
    mysql_connect("localhost" , "-------" , "--------");//change server name //pass username according your settings 

    mysql_select_db("--------");// also chang the Mysql database name 
    mysql_query("SET NAMES 'utf8'"); 
    mysql_query('SET CHARACTER SET utf8'); 
    $sql1=mysql_query("SELECT * FROM tbl_user WHERE email='$username' AND password='$password'"); 

    if (!$sql1) { 

    echo "Could not successfully run query ($sql) from DB: " . mysql_error(); 

    exit; 

    } 

    while($row=mysql_fetch_assoc($sql1)) 

    $output[]=$row; 


    //print(json_encode($output, JSON_UNESCAPED_UNICODE));// this will print the output in json 
    echo preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($output)); 

    mysql_close(); 

} 
+1

あなたが試したことと遭遇した具体的な問題を説明してください。 – CommonsWare

+2

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ –

+0

POSTメソッドを使用してユーザー名とパスワードをPHPファイルに送信します。このユーザーが有効な場合は別のフィールドが返されますこのjsonarrayコードを入手できますか? –

答えて

1

です:

JSONObject loginJSon = new JSONObject(); 
//then put your parameter to this jsonobejct,maybe username and password 
//loginJson.put("username",<username>) 
this.requestQueue.add(new JsonObjectRequest(<URL_LOGIN>,//your login url 
      loginJson, new Listener<JSONObject>() { 
       public void onResponse(JSONObject easyJsonObject) { 
        //here can get response from your server,just take the value as its key 
         String uid = easyJsonObject.getString("uid")); 

       } 
      }, this)); 
+0

しかし、私はjsonarray –

+0

がこの行にエラーが発生します。 文字列uid = easyJsonObject.getString( "uid")); 文字列は使用できません。 –

+0

私のPHPコードはmypostにあります –

0
try { 
         JSONArray jsonArray = new JSONArray(response); 
         for(int i = 0; i < jsonArray.length(); i++) 
         { 
          JSONObject jsonObj = jsonArray.getJSONObject(i); 
          if (!jsonArray.isNull(0)){ 
           String name=jsonObj.getString("name"); 

          }else { 
           //If the server response is not success 
           //Displaying an error message on toast 

          }; 

         } 
        } catch (JSONException e) { 
         //error 
        } 
関連する問題