0
を使用してPHPページからの応答を取得できません:私は私のPHPは、ブラウザでの作業を参照することができます私は私のコードを実行した場合、私はこのJSONExeptionエラーを取得アンドロイドスタジオ
"of type java.lang.String cannot be converted to JSONObject"
をしかしから何かを得ることができませんアプリ自体ユーザー名とパスワードを送信し、ユーザーが存在する場合はユーザー名を取得しようとしています。これは私の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を返そうとしています –