我正在学习android。我想在这里实现的是服务器和Android设备之间的简单连接。
我正在使用WAMP并已安装它。当我尝试通过浏览器(使用邮递员)点击php页面(服务器脚本从db检索)时,我能够实现我想要的。
我为下面的网址配置了我自己的网络ip,它在wamp中工作得很好,并且它也适用于postman。我正在使用一个真正的Android设备,它与我的系统(wifi)的连接相同
我的代码:我有两个活动和相应的xmls
我有一个名为BackgroundTask的类来执行事务
BackgroundTask 类:
public class BackgroundTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://<my ip here>/<myproject>/register.php";
String login_url = "http://<my ip here>/<myproject>/login.php";
String method = params [0];
if(method.equals("register"))
{
String firname = params[1];
String lasname = params[2];
String ages = params[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("firstname","UTF-8")+ "="+URLEncoder.encode(firname,"UTF-8")+ "&" +
URLEncoder.encode("lastname","UTF-8")+ "="+URLEncoder.encode(lasname,"UTF-8")+ "&" +
URLEncoder.encode("age","UTF-8")+ "="+URLEncoder.encode(ages,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
int responsecode = httpURLConnection.getResponseCode();
if (responsecode >= 400 && responsecode <= 499) {
throw new Exception("Bad authentication status: " + responsecode); //provide a more meaningful exception message
}
else {
//Get response from the server
InputStream IS = httpURLConnection.getInputStream();
IS.close();
}
return "Registration Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_SHORT).show();
}
}
注册活动:
public class register extends AppCompatActivity {
EditText fname, lname, age;
Button send;
String Firstname,Lastname,Age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initializevars();
}
public void initializevars() {
fname = (EditText) findViewById(R.id.etFirstName);
lname = (EditText) findViewById(R.id.etLastName);
age = (EditText) findViewById(R.id.etAge);
send = (Button) findViewById(R.id.btnSubmit);
}
public void userReg(View view)
{
Firstname = fname.getText().toString();
Lastname = lname.getText().toString();
Age = age.getText().toString();
String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,Firstname,Lastname, Age);
finish();
}
}
错误消息:
Bad Authentic状态:代码的这部分中的403
int responsecode = httpURLConnection.getResponseCode();
if(responsecode&gt; = 400&amp;&amp; responsecode&lt; = 499){
抛出新的异常(&#34;错误的身份验证状态:&#34; +响应代码); //提供更有意义的异常消息 }
否则{
//从服务器获取响应
InputStream IS = httpURLConnection.getInputStream();
IS.close(); }
到目前为止我尝试了什么:
我查看了几个stackoverflow的答案,但无法弄清楚问题。我尝试使用
getErrorstream
我没有收到错误消息,但我的数据没有到达数据库。
如果我的服务器脚本需要其他信息,请告诉我,我也会添加它们
请求您的指导和支持。
由于
编辑
我的php脚本
的init.php
<?php
$db_name = "dbname";
$mysql_user = "root";
$mysql_pass = "<mypassword here>";
$server_name = "localhost";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
{
//echo "Connection Error".mysqli_connect_error();
}else
{
//echo "<h3> Connection Success </h3>";
}
?>
register.php
<?php
require "init.php";
$first_name = $_POST["firstname"];
$last_name = $_POST["lastname"];
$age = $_POST["age"];
$sql_query = "insert into details values ('$first_name','$last_name','$age');";
if(mysqli_query($con,$sql_query))
{
//echo "<h3> data insertion successful </h3>";
} else
{
//echo "Error".mysqli_error($con);
}
?>
POSTMAN&amp; amp;屏幕截图DB
W hen我用我的ip替换localhost,它也能正常工作。在db pic中,你可以看到2条记录。我的错。我点击发送&#39;邮递员按钮两次
编辑2 -
我尝试使用Android模拟器而不是物理设备。将ip设置为模拟器的ip(10.0.x.x)它就像一个魅力。我也能在db中看到记录。
根据@ cricket_007的建议,我觉得它的ip问题。在这方面的任何帮助将是非常有帮助的。感谢