她是我使用网络服务注册的代码,一切正常但我不知道如何从网络服务获得json响应状态
Register.java
package com.example.limitscale.beautylog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
public class Register extends Activity {
private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private EditText editTextMobile;
private Button buttonRegister;
private static final String REGISTER_URL = "http://beauty.limitscale.com/webservices/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editTextName = (EditText) findViewById(R.id.name);
editTextUsername = (EditText) findViewById(R.id.username);
editTextPassword = (EditText) findViewById(R.id.password);
editTextEmail = (EditText) findViewById(R.id.email);
editTextMobile = (EditText) findViewById(R.id.mobile);
buttonRegister = (Button) findViewById(R.id.register);
buttonRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name=editTextName.getText().toString();
String uname=editTextUsername.getText().toString();
String password=editTextPassword.getText().toString();
String email=editTextEmail.getText().toString();
String mobile=editTextMobile.getText().toString();
if(Utility.isNotNull(name) && Utility.isNotNull(email) && Utility.isNotNull(password)
&& Utility.isNotNull(mobile) && Utility.isNotNull(uname)){
// When Email entered is Valid
registerUser();
Intent intentSignUP = new Intent(Register.this, MainActivity.class);
startActivity(intentSignUP);
}
else {
Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
}
}
});
}
private void registerUser() {
String name = editTextName.getText().toString().trim().toLowerCase();
String username = editTextUsername.getText().toString().trim().toLowerCase();
String password = editTextPassword.getText().toString().trim().toLowerCase();
String email = editTextEmail.getText().toString().trim().toLowerCase();
String mobile = editTextMobile.getText().toString().trim().toLowerCase();
register(name,username,password,email,mobile);
}
private void register(String name, String username, String password, String email,String mobile) {
class RegisterUser extends AsyncTask<String, Void, String>{
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Register.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("name",params[0]);
data.put("username",params[1]);
data.put("password",params[2]);
data.put("email",params[3]);
data.put("mobile",params[4]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(name,username,password,email,mobile);
}}class RegisterUserClass {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}}
这里当点击注册按钮重定向到下一个活动时,即使显示用户已经存在的文本,它也会进入下一步,所以我只想在json状态为真时重定向到下一个活动
答案 0 :(得分:3)
这是因为您的代码结构如下:
registerUser();
Intent intentSignUP = new Intent(Register.this, MainActivity.class);
startActivity(intentSignUP);
这意味着无论结果如何,您的用户仍将被重定向到MainActivity。
相反,如果JSON状态为true,则应在onPostExecute()中完成重定向。否则,请勿重定向并提供错误消息。因此,请尝试按以下步骤更新您的代码。
if(Utility.isNotNull(name) && Utility.isNotNull(email) && Utility.isNotNull(password)
&& Utility.isNotNull(mobile) && Utility.isNotNull(uname)){
// When Email entered is Valid
registerUser();
}
在你的onPostExecute(),
中@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
//do your JSON status checking here.
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}