问题解决了!!!
我在下面发布我的php文件和android类。 问题是当我尝试检查从PHP返回的结果时,它给了我很多垃圾数据。
loginStatus.php
<?php
$mysql = new mysqli("10.0.0.2", "root", "nishad0963", "cabService");
if($mysql->connect_errno){
echo "Failed to connect to DB";
}
//echo $mysql->host_info . "\n";
$userName = $_POST['userName'];
$password = $_POST['password'];
$query_select = $mysql->query("SELECT status FROM loginStatus WHERE userName = 'nishad' and password = 'password' ");
//echo $query_select;
while(($row = $query_select->fetch_assoc())!=null);
{
printf("result = %s", $row["status"]);
}
$mysql->close();
?>
signInActivity.java
package com.example.helloworld;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class signInActivity extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
//preparing data
String data = null;
String userName = (String) arg0[0];
String password = (String) arg0[1];
try {
data = URLEncoder.encode("userName", "UTF-8")+ "=" + URLEncoder.encode(userName, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("DATA_ERROR", e.getMessage());
e.printStackTrace();
}
//making connection
String url_address = "http://10.0.0.2/cabService/loginStatus.php";
try {
URL url = new URL(url_address);
URLConnection connect = url.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
Log.d("Progress Status", "It's working");
BufferedReader br = new BufferedReader(new InputStreamReader(connect.getInputStream()));
//reading response
StringBuilder sb = new StringBuilder();
String Line;
while((Line = br.readLine())!=null){
sb.append(Line);
Log.d("Result Status", Line);
}
return sb.toString();
} catch (MalformedURLException e) {
Log.e("URL_ERROR", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("URL_CONNECTION_ERROR", e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
return null;
}
protected void onPostExecute(String result) {
if(result.equals(1)){
Log.d("LOGIN_SUCCESS", result);
}
else
Log.d("LOGIN_FAILURE", result);
}
}
调试的logcat
01-03 16:42:52.102: D/Status(626): Before Clicking
01-03 16:42:52.452: D/Progress Status(626): It's working
01-03 16:42:52.532: D/Result Status(626): result =
结果状态是返回结果的标记。
希望你能帮忙! 任何建议将不胜感激。我认为错误在于PHP文件。
编辑:
mainActivity.java
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText userName, passWord;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.nameEdit);
passWord = (EditText) findViewById(R.id.passwordEdit);
final String name = userName.getText().toString();
final String password = passWord.getText().toString();
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("Status","Before Clicking");
new signInActivity().doInBackground(name, password);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
查看asyncTask传递的参数。我很确定这不正确。
答案 0 :(得分:0)
你的logcat输出说明了它; loginStatus.php
的第9行:
Function name must be a string.
您正在使用函数调用语法访问超级全局$_POST
,这是一个数组
$_POST('user')
代替$_POST['user']
。因为php允许基于字符串的函数调用,$myfuncname ="someFunc";
$myfuncname();
和$_POST
不是字符串而是数组,您会收到此错误消息。
答案 1 :(得分:0)
使用以下链接中的脚本完成同样的操作,这应该有所帮助。