【发布时间】:2015-05-01 14:15:32
【问题描述】:
Android JSON 没有响应 PHP JSON 保存我的记录
public class SignupActivity extends Activity {
Context _context;
EditText _Name;
EditText _Email;
EditText _Password;
Button _btnlogin;
public String getName;
public String getEmail;
public String getPassword;
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
public static String Path = "http://10.0.2.2/ViewApi/index.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_context = this;
setContentView(R.layout.activity_signup);
_Name = (EditText) findViewById(R.id.username_id);
_Email = (EditText) findViewById(R.id.email_id);
_Password = (EditText) findViewById(R.id.pass_id);
_btnlogin = (Button) findViewById(R.id.btn_submit);
_btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
getName = _Name.getText() + "";
getEmail = _Email.getText().toString().trim();
getPassword = _Password.getText() + "";
if ((_Name.toString() != null) && (_Email.toString() != null) && (_Password.toString() != null)) {
if ((_Name.length() == 0) && (_Email.length() == 0) && (_Password.length() == 0)) {
Toast.makeText(getApplicationContext(), "All Fields are Empty", Toast.LENGTH_SHORT).show();
} else if ((_Name.length() == 0) || (_Email.length() == 0) || (_Password.length() == 0)) {
Toast.makeText(getBaseContext(), "One or more Fields are Empty", Toast.LENGTH_SHORT).show();
}
/*
else if ((_Name.toString()=="Name") && (_Password.toString()=="Email") && (_Password.toString()=="Password"))
{
_Name.setError("Name Must be greater than 6 Characters");
_Email.setError("Email Format is Wrong");
_Password.setError("Password must be Greater than 6 Characters");
}
*/
else if ((_Name.toString() != "Name") && (_Email.toString() != "Email") && (_Password.toString() != "Password")) {
if (_Name.length() < 6) {
_Name.setError("Name Must be greater than 6 Characters");
}
//if (getEmail.toString() != emailPattern) {
// _Email.setError("Invalid Email");
//}
if (_Password.length() < 6) {
_Password.setError("Password must be Greater than 6 Characters");
}
}
} else if ((_Name.length() > 6) && (_Email.toString() == emailPattern) && (_Password.length() > 6)) {
getName = _Name.getText().toString();
getEmail = _Email.getText().toString();
getPassword = _Password.getText().toString();
SignupJSONPost();
Log.i("Name", getName);
Log.i("Email", getEmail);
Log.i("Password", getPassword);
}
}
});
}
public void SignupJSONPost() {
HttpClient mhttpclient = new DefaultHttpClient();
HttpPost mhttppost = new HttpPost(Path);
HttpParams mhttpparams = new BasicHttpParams();
JSONObject json = new JSONObject();
try {
// json.put("name",getName);
// json.put("id",getEmail);
// json.put("pass",getPassword);
List<NameValuePair> nameValuePairList = new ArrayList<>(1);
nameValuePairList.add(new BasicNameValuePair("json",json.toString()));
nameValuePairList.add(new BasicNameValuePair("name", getName));
nameValuePairList.add(new BasicNameValuePair("email", getEmail));
nameValuePairList.add(new BasicNameValuePair("password", getPassword));
HttpConnectionParams.setConnectionTimeout(mhttpparams, 10000);
HttpConnectionParams.setSoTimeout(mhttpparams, 10000);
//mhttppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
mhttppost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
//mhttppost.setHeader("json", json.toString());
mhttppost.addHeader("Content-Type", "application/json");
HttpResponse mhttpresponse =mhttpclient.execute(mhttppost);
HttpEntity mhttpentity = mhttpresponse.getEntity();
if (mhttpentity!=null)
{
InputStream minstream = mhttpentity.getContent();
//String res = minstream.convertStreamToString();
Log.i("Server response",minstream.toString());
Toast.makeText(this,minstream.toString(),Toast.LENGTH_LONG).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}
// catch (JSONException e)
// {
// e.printStackTrace();
//}
catch (Throwable t)
{
t.printStackTrace();
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
页面:
<?php
header("Content-Type:application/json");
include("function.php");
if(!empty($_GET['name']) && !empty($_GET['id']) && !empty($_GET['pass']))
{
$name = $_GET['name'];
$id = $_GET['id'];
$pass = $_GET['pass'];
$record = fetchrecord($name,$id,$pass);
if(empty($record))
{
deliverresponse(200,"Registration failed",NULL);
}
else
{
deliverresponse(200,"Registration Successfull",$record);
}
}
else
{
deliverresponse(400,"Invalid Request",NULL);
}
function deliverresponse($status,$status_message,$data)
{
header("HTTP/1.1 $status $status_message");
$response['status'] = $status;
$response['status_message'] = $status_message;
$response['data'] = $data;
$jsonresponse = json_encode($response);
echo $jsonresponse;
}
?>
我的 Android 代码没有响应任何东西,Toast 也没有调用按钮,请点击一些我帮助我...
【问题讨论】:
-
您遇到任何错误吗?
-
按下按钮时连吐司都没有显示...