2016-05-05 140 views
0

如果我跑我的代碼,我得到這個JSONExeption錯誤擺脫一個PHP頁面的任何響應:無法使用機器人工作室

"of type java.lang.String cannot be converted to JSONObject"

我可以看到我的PHP在瀏覽器中工作,但我無法從得到什麼應用本身。我正在嘗試發送用戶名和密碼,並在用戶存在時檢索用戶名。這是我的PHP代碼:

if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$username = $_GET['username']; 
$password = $_GET['password']; 
$result = mysqli_query($con,"SELECT email FROM user where 
email='$username' and password='$password'"); 
$row = mysqli_fetch_array($result); 
$data = $row[0]; 

if($data){ 
echo json_encode($data); 
} 
else 
{ 
    echo json_encode("false"); 
    //print "false"; 
} 
mysqli_close($con) 

這是在應用程序代碼:

public String doWebService(String name, String pass) 
{ 
Log.i(activityName, "Thread started; attempting to invoke web service"); 

String a = name; 
String b = pass; 
String sum = "Nothing"; 


HttpURLConnection con = null; 
String responseStr = ""; 


try 
{ 

    //****************************************************************// 
    // Create a JSON object and use it to encode two integers with // 
    // the keys "paramA" and "paramB".        // 
    //****************************************************************// 
    JSONObject toSendObject = new JSONObject(); 
    toSendObject.put("username", a); 
    toSendObject.put("password", b); 
    //String jsonStr = URLEncoder.encode(toSendObject.toString(), "UTF-8"); 
    String jsonStr = toSendObject.toString(); 




    Log.i(activityName, "JSON str" + jsonStr); 
    //String urlStr = webServiceURL + "?" + jsonStr; 
    String urlStr = webServiceURL + "?username=" + a +"&password=" + b; 
    Log.i(activityName, "Sending this to web service: " + urlStr); 
    URL url = new URL(urlStr); 



    //********************************************************// 
    // Create an HTTP connection, with various attributes. // 
    //********************************************************// 
    con = (HttpURLConnection) url.openConnection(); 
    con.setReadTimeout(10000); 
    con.setConnectTimeout(15000);  //Timeout in milliseconds. 
    con.setRequestMethod("GET");  //Use HTTP GET 
    con.setChunkedStreamingMode(0); //Default 
    con.setDoInput(true);    //So that we can read response back 
    con.connect(); 



    //********************************************************// 
    // Now read the response.         // 
    //********************************************************// 
    BufferedReader in = new BufferedReader(
      new InputStreamReader(con.getInputStream(), "UTF-8")); 


    responseStr = in.readLine(); 

    in.close(); 
    con.disconnect(); 

    //******************************************************************// 
    //Now parse the JSON result to get the sum.      // 
    //******************************************************************// 

    Log.i(activityName, "Response str: " + responseStr); 
    JSONObject result = new JSONObject(responseStr); 
    sum = result.toString(); 

    Log.i(activityName, "Response " + sum); 
} 

回答

0

什麼做瀏覽器中的PHP代碼的回報?

+0

正如你可以在上面的代碼中看到的,我試圖返回變量$ data –