2016-09-07 51 views
0

我在我的android應用程序中使用Volley庫。我正在嘗試創建註冊功能。一切似乎工作正常,我可以註冊,但我無法顯示任何響應從PHP文件作爲敬酒在Android上。吐司甚至沒有出現。也許我應該先告訴你我的代碼:作爲吐司文本顯示排列反應

Response.Listener<String> listener = new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
     try { 
       JSONObject jsonResponse = new JSONObject(response); 
       String result = jsonResponse.getString(response); 

       if(result == "Registered!") { 
        Toast.makeText(RegActivity.this, result, Toast.LENGTH_LONG).show(); 
        } else { 
        Toast.makeText(getApplicationContext(),"Could not register!", Toast.LENGTH_LONG).show(); 
        } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
       } 
      } 
     }; 
    RegRequest regRequest = new RegRequest(username, password, email, listener); 
    RequestQueue queue = Volley.newRequestQueue(RegActivity.this); 
    queue.add(regRequest); 

這裏是我的regRequest類:

public class RegRequest extends StringRequest { 

private static String regURL = "http://chicpiq.com/android/android_register.php"; 
private Map<String, String> params; 

public RegRequest(String username, String password, String email, Response.Listener<String> listener) { 
    super (Method.POST, regURL, listener, null); 
    params = new HashMap<>(); 
    params.put("username", username); 
    params.put("password", password); 
    params.put("email", email); 
} 

@Override 
public Map<String, String> getParams() { 
    return params; 
    } 
} 

這裏是我的PHP:

require "../database/database.php"; 

$username = mysqli_real_escape_string($database, $_POST['username']); 
$password = mysqli_real_escape_string($database, $_POST['password']); 
$email = mysqli_real_escape_string($database, $_POST['email']); 
$joindate = date("y.m.d"); 

$encryptpass = password_hash($password, PASSWORD_DEFAULT); 
$register = mysqli_query($database, "INSERT INTO users (username, password, email, joindate) VALUES ('$username', '$encryptpass', '$email', '$joindate')"); 
mysqli_execute($register); 
$response = "Registered!"; 

echo json_encode($response); 

正如我所說,我有註冊沒有問題,我可以做到這一點。我的問題是我想更改PHP代碼來檢查用戶是否存在等,我想顯示其他消息。我似乎無法顯示來自PHP的回覆作爲吐司。有沒有人有任何想法? Cheers

回答

0
JSONObject js = new JSONObject(); 
    try { 

     js.put("username", username); 
     js.put("password", password); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, constants.LOG_IN_URL, js, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        try { 
         System.out.println("status" + response.get("status").toString().contains("success")); 
         if (response.get("status").toString().contains("success")) { 

          profileId.setText(response.getJSONArray("data").getJSONObject(0).getString("fname")); 
          userProfession.setText(response.getJSONArray("data").getJSONObject(0).getString("profession")); 
          userLocation.setText(response.getJSONArray("data").getJSONObject(0).getString("placeOfBirth")); 
          String url = "http://50.78.27.70:8080"; 
          url =url.concat(response.getJSONArray("data").getJSONObject(0).getString("displayPicture")); 
          System.out.println("string" + response.getJSONArray("data").getJSONObject(0).getString("displayPicture")); 
          if(response.getJSONArray("data").getJSONObject(0).getString("displayPicture").equals("null")) 
          { 
            profilePic.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

          } 
          else{ 
           Glide.with(getBaseContext()).load(url).into(profilePic); 
           System.out.println("hello by default 2"); 
          } 




         } else { 
         } 
        } catch (JSONException e) { 

         e.printStackTrace(); 
        } 


       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(TimeLine.this, "Wrong Id or Password", Toast.LENGTH_SHORT).show(); 
        error.printStackTrace(); 
       } 
      }); 


    Singleton.mInstance(TimeLine.this).addrequestqueue(request);