【问题标题】:Unable to get any responses from a php page using android studio无法从使用 android studio 的 php 页面获得任何响应
【发布时间】:2016-05-05 16:04:08
【问题描述】:

如果我运行我的代码,我会收到 JSONExeption 错误:

“java.lang.String 类型的无法转换为 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);
}

【问题讨论】:

    标签: java php android json mysqli


    【解决方案1】:

    您的 php 代码在浏览器中返回什么?

    【讨论】:

    • 正如您在上面的代码中看到的,我正在尝试返回变量 $data
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2014-01-30
    相关资源
    最近更新 更多