gpt4 book ai didi

php - 没有 HTTP 响应的 HTTP 请求

转载 作者:行者123 更新时间:2023-11-29 02:26:39 24 4
gpt4 key购买 nike

我只需要知道我的申请中缺少什么我的 Android 应用程序将用户名和密码作为 HTTP POST 发送,然后 PHP 文件使用数据库中该用户名的值进行响应问题是当我输入用户名和密码时,Toast 中什么也没有返回,它甚至没有显示

主要 Activity

package com.example.postapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;



import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView tv;
EditText u,p;
Button login;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


login = (Button) findViewById(R.id.login);

login.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {

u = (EditText) findViewById(R.id.username);
p = (EditText) findViewById(R.id.password);


Toast.makeText(getApplicationContext(), u.getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), p.getText().toString(), Toast.LENGTH_LONG).show();
new MyAsyncTask().execute(u.getText().toString(),p.getText().toString());

}

});


}

@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;
}
public class MyAsyncTask extends AsyncTask<String, Integer, Boolean>{
String host;


@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean a= postData(params[0],params[1]);
return a;
}

protected void onPostExecute(Boolean localres){
tv = (TextView) findViewById(R.id.tv);
if (localres){
tv.setText("A Correct Username and Password");
}else{
tv.setText("Incorrect Username or Password");
}
// Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_SHORT).show();
if(host!=null)
{
// Toast.makeText(getApplicationContext(), host.toString(), Toast.LENGTH_LONG).show();
}
}

protected void onProgressUpdate(Integer... progress){
//pb.setProgress(progress[0]);
//Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();

}


public Boolean postData(String a,String b) {
// ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// postParameters.add(new BasicNameValuePair("username", a));
// postParameters.add(new BasicNameValuePair("password", b));
HttpURLConnection connection;
OutputStreamWriter request = null;

URL url = null;
String response = null;
String parameters = "username="+a+"&password="+b;

try
{
url = new URL("http://"+"192.168.1.3"+"/new/check2.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();
isr.close();
reader.close();

}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}


}



}

PHP

<?php

$con = mysqli_connect("localhost", "root", "123", "pet_home");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("select * from users where username='$username' and password='$password'")or die (mysql_error());
$count = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if ($count > 0) {
echo $row['filter_st'];
echo "<br>";
echo $row['heat_st'];
echo "<br>";
echo $row['led_st'];
} else {
echo 0;
}

mysqli_close($con);

Logcat出现异常

12-13 20:57:45.534: W/System.err(14768): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-13 20:57:45.534: W/System.err(14768): at android.os.Handler.<init>(Handler.java:197)
12-13 20:57:45.534: W/System.err(14768): at android.os.Handler.<init>(Handler.java:111)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast$TN.<init>(Toast.java:324)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast.<init>(Toast.java:91)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast.makeText(Toast.java:238)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.postData(MainActivity.java:136)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:76)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
12-13 20:57:45.538: W/System.err(14768): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-13 20:57:45.538: W/System.err(14768): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-13 20:57:45.538: W/System.err(14768): at java.lang.Thread.run(Thread.java:841)

最佳答案

Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();          

你有

Boolean a= postData(params[0],params[1]); // in doInbackground

public Boolean postData(String a,String b) 中,你显示的 toast 是错误的

您正在后台线程中更新用户界面,这是不可能的。在 ui 线程上更新 ui。

doInbackground 中返回结果。 doInbackground 计算的结果是 onPostExecute 的参数。因此,您可以根据结果更新 ui,即相应地显示 toast。

或者使用runOnUiThread

runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();
}
});

关于php - 没有 HTTP 响应的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com