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);
}
正如你可以在上面的代码中看到的,我试图返回变量$ data –