我是Android开发的新手。正在制作应用程序的位置,用户将通过Android中的注册表单向我们注册。但是我没有得到任何错误,也没有得到输出。
这是java的源代码:
package com.example.entrepreneurexpress;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InvestorRegister extends Activity {
EditText YourName;
EditText email;
EditText password;
EditText confirmPassword;
Button btnClear, btnRegister;
String nm, emailAdd, cnfPass, pwd;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.investors_registration);
ActionBar aBar = getActionBar();
aBar.setDisplayHomeAsUpEnabled(true);
YourName = (EditText) findViewById(R.id.invRegName);
email = (EditText) findViewById(R.id.invRegEmail);
password = (EditText) findViewById(R.id.invRegPassword);
confirmPassword = (EditText) findViewById(R.id.invRegConfPassword);
btnClear = (Button) findViewById(R.id.btnInvRegClear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (YourName.length() >= 1) {
YourName.setText("");
}
if (email.length() >= 1) {
email.setText("");
}
if (password.length() >= 1) {
password.setText("");
}
if (confirmPassword.length() >= 1) {
confirmPassword.setText("");
}
}
});
btnRegister = (Button) findViewById(R.id.btnInvRegRegister);
btnRegister.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(
(!YourName.getText().toString().equals("")) &&
(!email.getText().toString().equals("")) &&
(!password.getText().toString().equals(""))
) {
/*Toast.makeText(getApplicationContext(), "Registered !", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), WelcomeInvestor.class);
startActivity(i);*/
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://122.170.107.27/AdroidApp/insert.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("invName", YourName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("invPassword", password.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("invEmail", email.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
int status = response.getStatusLine().getStatusCode();
if(status == 200) {
Toast.makeText(getApplicationContext(), "Registered !", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), WelcomeInvestor.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Some Error Occured !", Toast.LENGTH_SHORT).show();
}
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "Error "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
thread.start();
} else {
Toast.makeText(getApplicationContext(), "Please Fill In All The Details!", Toast.LENGTH_SHORT).show();
}
}
});
}
这是php代码:
<?php
$output = array();
$con = mysqli_connect("localhost", "root", "root", "AndroidApp");
if(!$con) {
echo "Connection Aborted !<br />" . mysqli_error($con);
} else {
echo "Connection Successful !<br />";
}
$name = isset($_POST['fullname']) ? $_POST['fullname'] : '';
$email = isset($_POST['emailAddress']) ? $_POST['emailAddress'] : '';
$pasword = isset($_POST['passWord']) ? $_POST['passWord'] : '';
$query = mysqli_query($con, "INSERT INTO Investors
(`invName`, `invEmail`, `invPassword`)
VALUES('".$name."', '".$email."', '".$pasword."')");
if ($query)
{
$output["success"] = 1;
$output["message"] = "Successfully Inserted";
echo jsonencode($output);
} else {
$output["success"] = 0;
$output["message"] = "insertion failed......";
echo json_encode($output);
}
mysqli_close($con);
?>
更新java代码后现在收到此错误:
04-10 14:02:26.403: E/linker(29292): load_library(linker.cpp:759): library "libmaliinstr.so" not found
我尽了最大努力,但失败了。请帮助我。感谢。
答案 0 :(得分:0)
首先,Strict模式仅用于开发目的,进行调试。并且在发布时不要使用。所有网络通信必须在主线程之外进行。如果你使用严格模式 - permitAll(),你正在搞乱正常操作。因此,使用AsynchTask进行任何您想要的网络通信。
你可以在这里找到教程。
http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask
此外,您在执行httpclient.execute(httppost);
时会直接尝试获取响应。我认为这不会起作用,因为服务器需要时间来响应。
在AsynchTask中,你有一个方法onPostExecute
可以正确处理这个问题。
阅读本文 http://blog.vogella.com/2012/02/22/android-strictmode-networkonmainthreadexception/ http://blog.vogella.com/2011/04/04/android-strictmode/