您好我正在开发一个Android应用程序,我希望用户登录到应用程序,在登录期间,我将使用共享首选项保存服务器中的所有数据。我只是显示了错误的网址错误和错误404.我不知道它是什么。我的代码是。
public class MainActivity extends Activity {
private ProgressDialog pDialog;
private EditText userNameET, userPassET;
private Button loginBtn;
private String userType = "";//Created by Ranjani
private String statusinfo="";
private String url = "";
private static String TAG = MainActivity.class.getSimpleName();
private String carAssigningStatus,customerAssigningStatus,driverAssigningStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
userNameET=(EditText) findViewById(R.id.userNameId);
userPassET=(EditText) findViewById(R.id.passwordId);
loginBtn=(Button) findViewById(R.id.loginBtnId);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userNameString = userNameET.getText().toString();
String userPassString = userPassET.getText().toString();
if(userNameString == "")
Toast.makeText(getApplicationContext(), "Enter Username", Toast.LENGTH_LONG).show();
else
if(userPassString == "")
Toast.makeText(getApplicationContext(), "Enter Password", Toast.LENGTH_LONG).show();
else
{
url = "http://www.imaginetventures.net/sample/goodmorning/rest/?type=login&username="+ userNameString+"&password="+userPassString;
//Getting information from the webserver
GetInfofromserver();
}
}
});
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
protected void GetInfofromserver() {
// TODO Auto-generated method stub
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
JSONObject logObj = response.getJSONObject("login");
JSONArray jArraystatus = logObj
.getJSONArray("status");
// For Getting Status
for (int i = 0; i < jArraystatus.length(); i++) {
JSONObject status = jArraystatus
.getJSONObject(i);
statusinfo = status.getString("user_status");
if (statusinfo.equalsIgnoreCase("no")) {
Toast.makeText(
getApplicationContext(),
"Username / Password does not match",
Toast.LENGTH_LONG).show();
}
}
if (statusinfo.equalsIgnoreCase("yes")) {
JSONArray jArraytype = logObj
.getJSONArray("type");
JSONArray jArraydriver = logObj
.getJSONArray("driver");
JSONArray jcustomerArray = logObj
.getJSONArray("customer");
JSONArray jcarArray = logObj
.getJSONArray("car");
// For Getting Usertype Info
for (int i = 0; i < jArraytype.length(); i++) {
JSONObject type = jArraytype
.getJSONObject(i);
userType = type.getString("user_type");
SharedPreferences settings = getSharedPreferences(
"MyPreferences", 0);
SharedPreferences.Editor editor = settings
.edit();
editor.putString("Login_userType", userType);
editor.putString("login_Shift_Status", "no");
editor.commit();
// Log.d("*****===== ", "> " +userType);
}
switch (userType) {
case "driver": {
for (int i = 0; i < jArraydriver.length(); i++) {
JSONObject driver = jArraydriver.getJSONObject(i);
if(driver.has("user_id"))
{
String user_Id = driver
.getString("user_id");
String name = driver.getString("name");
String phone = driver
.getString("phone");
String photo = driver
.getString("photo");
String points = driver
.getString("points");
SharedPreferences settings = getSharedPreferences(
"MyPreferences", 0);
SharedPreferences.Editor editor = settings
.edit();
editor.putString("driver_user_id",
user_Id);
editor.putString("driver_name", name);
editor.putString("driver_phone", phone);
editor.putString("driver_photo", photo);
editor.putString("driver_points",
points);
// editor.putString("driver_login_status",
// login_status);
editor.commit();
}
}
for(int inc=0;inc<jcustomerArray.length();inc++)
{
JSONObject customer=jcustomerArray.getJSONObject(inc);
if( customer.has("assign_status"))
{
customerAssigningStatus=customer.getString("assign_status");
if(customerAssigningStatus.equalsIgnoreCase("yes"))
{
JSONObject getcustomer=jcustomerArray.getJSONObject(inc-1);
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
String user_Id = getcustomer.getString("user_id");
String name = getcustomer.getString("name");
//String gender = c1.getString("gender");
//String company = c1.getString("company");
String email = getcustomer.getString("email");
//String address = c1.getString("address");
String phone = getcustomer.getString("phone");
String photo = getcustomer.getString("photo");
editor.putString("customerAssigningStatus",customerAssigningStatus );
editor.putString("customer_user_id", user_Id);
editor.putString("customer_name", name);
//editor.putString("customer_gender", gender);
//editor.putString("customer_company", company);
editor.putString("customer_email", email);
//editor.putString("customer_address", address);
editor.putString("customer_phone", phone);
editor.putString("customer_photo", photo);
editor.commit();
}
else
{
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("customerAssigningStatus",customerAssigningStatus );
editor.commit();
}
}
}
//To Check whether the car have been assigned or not
for(int inc=0;inc<jcarArray.length();inc++)
{
JSONObject car=jcarArray.getJSONObject(inc);
if( car.has("assign_status"))
{
carAssigningStatus=car.getString("assign_status");
if(carAssigningStatus.equalsIgnoreCase("yes"))
{
JSONObject getcar=jcarArray.getJSONObject(inc-1);
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
String carId = getcar.getString("car_id");
String name = getcar.getString("car_make");
String model = getcar.getString("car_model");
String car_type = getcar.getString("car_type");
String color = getcar.getString("color");
String car_reg_no = getcar.getString("car_reg_no");
String model_year = getcar.getString("model_year");
String photo = getcar.getString("photo");
String mileage = getcar.getString("mileage");
Log.d("mileage ===== ","");
editor.putString("carAssigningStatus",carAssigningStatus );
editor.putString("car_user_id", carId);
editor.putString("car_name", name);
editor.putString("car_model", model);
editor.putString("car_car_type", car_type);
editor.putString("car_color", color);
editor.putString("car_car_reg_no", car_reg_no);
editor.putString("car_model_year", model_year);
editor.putString("car_photo", photo);
editor.putString("car_mileage", mileage);
editor.commit();
}
else
{
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("carAssigningStatus",carAssigningStatus );
editor.commit();
}
}
}
break;
}
case "customer":
{
for (int i = 0; i < jcustomerArray.length(); i++) {
JSONObject customer = jcustomerArray.getJSONObject(i);
if(customer.has("user_id"))
{
String user_Id = customer.getString("user_id");
String name = customer.getString("name");
//String gender = c1.getString("gender");
//String company = c1.getString("company");
String email = customer.getString("email");
//String address = c1.getString("address");
String phone = customer.getString("phone");
String photo = customer.getString("photo");
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("customer_user_id", user_Id);
editor.putString("customer_name", name);
//editor.putString("customer_gender", gender);
//editor.putString("customer_company", company);
editor.putString("customer_email", email);
//editor.putString("customer_address", address);
editor.putString("customer_phone", phone);
editor.putString("customer_photo", photo);
editor.commit();
}
}
for(int inc=0;inc<jArraydriver.length();inc++)
{
JSONObject driver=jArraydriver.getJSONObject(inc);
if( driver.has("assign_status"))
{
driverAssigningStatus=driver.getString("assign_status");
if(driverAssigningStatus.equalsIgnoreCase("yes"))
{
JSONObject getdriver=jArraydriver.getJSONObject(inc-1);
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
String user_Id = getdriver.getString("user_id");
String name = getdriver.getString("name");
//String gender = c1.getString("gender");
//String company = c1.getString("company");
String email = getdriver.getString("email");
//String address = c1.getString("address");
String phone = getdriver.getString("phone");
String photo = getdriver.getString("photo");
String points=getdriver.getString("points");
String address=getdriver.getString("address");
editor.putString("driverAssigningStatus",driverAssigningStatus );
editor.putString("driver_user_id", user_Id);
editor.putString("driver_name", name);
editor.putString("driver_email", email);
//editor.putString("customer_address", address);
editor.putString("driver_phone", phone);
editor.putString("driver_photo", photo);
editor.putString("driver_address", address);
editor.putString("driver_points", points);
editor.commit();
}
else
{
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("driverAssigningStatus",driverAssigningStatus );
editor.commit();
}
}
}
//To Check whether the car have been assigned or not
for(int inc=0;inc<jcarArray.length();inc++)
{
JSONObject car=jcarArray.getJSONObject(inc);
if( car.has("assign_status"))
{
carAssigningStatus=car.getString("assign_status");
if(carAssigningStatus.equalsIgnoreCase("yes"))
{
JSONObject getcar=jcarArray.getJSONObject(inc-1);
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
String carId = getcar.getString("car_id");
String name = getcar.getString("car_make");
String model = getcar.getString("car_model");
String car_type = getcar.getString("car_type");
String color = getcar.getString("color");
String car_reg_no = getcar.getString("car_reg_no");
String model_year = getcar.getString("model_year");
String photo = getcar.getString("photo");
String mileage = getcar.getString("mileage");
Log.d("mileage ===== ","");
editor.putString("carAssigningStatus",carAssigningStatus );
editor.putString("car_user_id", carId);
editor.putString("car_name", name);
editor.putString("car_model", model);
editor.putString("car_car_type", car_type);
editor.putString("car_color", color);
editor.putString("car_car_reg_no", car_reg_no);
editor.putString("car_model_year", model_year);
editor.putString("car_photo", photo);
editor.putString("car_mileage", mileage);
editor.commit();
}
else
{
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("carAssigningStatus",carAssigningStatus );
editor.commit();
}
}
}
break;
}
default:
break;
}
SharedPreferences settings1 = getSharedPreferences("MyPreferences", 0);
String strValue1 = settings1.getString("car_mileage",null);
SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
String strValue = settings.getString("Login_userType",null);
if(TextUtils.equals(strValue, "customer")) {
Intent i = new Intent(getApplicationContext(),ClientTabMainActivity.class);
startActivity(i);
} else if (TextUtils.equals(strValue, "driver")) {
Intent i = new Intent(getApplicationContext(),ActivityDriverShiftView.class);
startActivity(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}
,错误是
03-02 12:09:33.212: E/Volley(23357): [21267] BasicNetwork.performRequest: Unexpected response code 404 for http://www.imaginetventures.net/sample/goodmorning/rest/?type=login&username=davidraja&password=123456
03-02 12:09:33.222: D/Volley(23357): [1] 3.onErrorResponse: MainActivity
注意:我为另一个网址编写了这段代码,但现在网址已被更改,旧网址未被使用。但是JSON数据和以前一样,但我不知道它为什么不起作用。