这是安卓代码在menifest中获取权限我关注了许多视频,但相同的结果请帮助我
Context ctx;
String res;
AlertDialog alertDialog;
background(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("hellooooo");
}
@Override
protected String doInBackground(String... params) {
String url_response = "http://192.168.1.102/login.php";
String id = params[0];
try {
URL url = new URL(url_response);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8");
bw.write(data);
bw.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
String line ="";
res = "";
while((line = br.readLine())!=null)
{
res +=line;
}
return res;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "ffffff";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
alertDialog.setMessage(result);
alertDialog.show();
}
php连接代码这是api的php代码,它是100%正确的,我在localhost上检查了它,当我试图通过这个php代码api连接到andriod-whith-mysql时,它是有效的,但它不起作用。你们有什么想法吗
$mysql_qry = "select * from users where name like '$id';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result)>0)
{
echo "success";
}
else
{
echo "not suces";
}
?>
输出误差在此处输入图像描述
您是否已将internet访问权限添加到清单中?
<uses-permission android:name="android.permission.INTERNET" />
此外,请尝试在请求中发送正确的内容类型:
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
我建议您使用截击库来进行http事务。你可以在这里阅读你需要的关于截击库的一切http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800
您需要在项目中添加截击库。在您的应用程序build.gradle中,根据dependencies添加此compile 'com.mcxiaoke.volley:library-aar:1.0.0'
。
创建一个对http请求使用截击的方法,忘记asyntask,因为截击已经处理了它。
public void setRequestToServer (String mId){
final String url_response = "http://192.168.1.102/login.php";
final String id = mId;
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(response);
StringRequest stringRequest = new StringRequest(Request.Method.POST, mUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response){
if (!TextUtils.isEmpty(response)){
alertDialog.setMessage(response);
}else{
alertDialog.setMessage("Cannot received response from server");
}
alertDialog.show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("on Failure", error+"");
alertDialog.setMessage(error+"");
alertDialog.show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("id",id));
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
在php脚本上,使用$id存储$_POST['id']
中的值,如下所示:
$id = $_POST['id'];
$mysql_qry = "select * from users where name like '$id'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result)>0)
{
echo "success";
}
else
{
echo "not suces";
}