Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can i arrive at the my expected output in my android project?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 66
    Answer it

    Hello friends how can i arrive at my expected output in my android project?

    Currently, I am getting all the output values such as brand,model,type,variant as integers instead of text. Actually i need my result as 

    RegNo: TN10D5551

    Brand:AUDI

    Model: AUDI A6

    Type:Diesel

    Variant:Fully loaded etc...,

    But i am currently getting the result as:

    RegNo:TN10D5551

    Brand: 1

    Model: 2

    Type:2

    Variant:1....

    Iam getting my results as the id values from my postman API response which is not needed for me. Herewith in this post i had posted my API response too

    My API's with methods and response

    4. Brand Master API :

    Link : http://garage.kaptastech.mobi/api/5k/master/brand

    Method : GET

    Description :  API to get brand lists.

    Response :

    {"status":1,"data":[{"id":1,"name":"AUDI","code":"AD","image":"","status":"1","created_at":"2016-09-14 13:09:36","updated_at":"2016-09-15 12:11:05"},{"id":3,"name":"BMW","code":"BMW","image":"","status":"1","created_at":"2016-09-15 13:23:41","updated_at":"2016-09-15 13:23:41"}],"msg":"success","info":"data list"}

     

    5. Car Model Master API :

    Link : http://garage.kaptastech.mobi/api/5k/master/models/{brand_id}

    Method : GET

    URL Params : brand_id : id from above API

    Description :  API to get car model lists.

    Response :

    If result found.

                   {"status":1,"data":[{"id":1,"brand_id":3,"name":"320 D","code":"320d","image":"","status":"1","created_at":"2016-09-22 15:46:50","updated_at":"2016-09-22 15:46:50"},{"id":2,"brand_id":3,"name":"520 D","code":"520d","image":"","status":"1","created_at":"2016-09-22 15:47:06","updated_at":"2016-09-22 15:47:06"}],"msg":"success","info":"data list"}

    If result not found.

                   {"status":0,"data":[],"msg":"success","info":"data list"}

     

     

     

     

    6. Add Vehicles API :

    Link :  http://garage.kaptastech.mobi/api/5k/master/vehicle

    Method : POST

    Body Param :

    user_id : integer - current logged user id

    registration_no : string - without any space

    brand : integer - from brand API

    model : integer - from car model API

    type : integer 1 or 2 -> 1 = PETROL , 2 = DIESEL

    variant : integer 1 or 2 -> 1 = Fully loaded , 2 = Basic Model

    year : integer - 4 digits

    Description : API to add vehicle for users.

    Response :

    If successfully added.

    {"status":1,"data":{"user_id":"2","registration_no":"TN38BQ2054","brand_id":"1","model_id":"4","type":1,"year":"2016","variant":1,"status":1,"updated_at":"2016-09-22 17:10:42","created_at":"2016-09-22 17:10:42","id":1},"msg":"success","info":"Record created"}

    If validation

                   {"status":2,"data":{"type":["The type must be a number."],"variant":["The variant must be a number."]},"msg":"error","info":"validation_error"}

    7. Fetch Vehicles API :

    Link : http://garage.kaptastech.mobi/api/5k/users/vehicle

    Method : GET

    Request Header Params :

    id : logged user_id

    imei : mobile imei number

    Description : API to fetch the list of vehicles.

    Response :

    When success : {"status":1,"data":[{"id":1,"user_id":2,"brand_id":1,"model_id":4,"registration_no":"TN38BQ2054","year":"2016","color":"","type":"","variant":"","door_mats":"0","audio_systems":"0","front_speaker":0,"back_speaker":0,"led_screen":"0","alloy":"0","status":"1","created_at":"2016-09-22 17:10:42","updated_at":"2016-09-22 17:10:42"}],"msg":"success","info":"records found"}

    When invalid imei :

    {"status":3,"data":[],"info":"you have logged in some other mobile, kindly login again","msg":"error"}

    When no data found :

                   {"status":0,"data":[],"info":"no records found","msg":"success"}

    I had also posted my screenshots of what my expectation is and my actual result is and also i am ready to upload my project zip file as an edit of this post if needed, Please help me,Thanks in advance.

     

    My java files: please note that Iam adding my vehicle in mainactivity and fetchnig it in addvehicleActivity.

    MainActivity.java

    package com.example.addvehicle;
    
    import android.content.Intent;
    
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.CompoundButton;
    import android.widget.EditText;
    
    import android.widget.RadioButton;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.appindexing.Thing;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.JSONArray;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    
    import static com.example.addvehicle.R.string.brand;
    
    public class MainActivity extends AppCompatActivity {
    
    
        private String TAG = MainActivity.class.getSimpleName();
        ArrayList<String> brandListArray = new ArrayList<String>();
        ArrayList<String> modelListArray = new ArrayList<String>();
        Button addVehicleBtn;
        EditText AddVehicle_Regno, kmscovered;
        RadioButton petrol, Diesel, fullyloaded, Basicmodel;
        TextView textView8, textViewkms;
        Spinner AddspinnerMake, AddspinnerModel, AddspinnerYear;
        private static String url = "http://garage.kaptastech.mobi/api/5k/master/vehicle";
        private String[] values;
        ArrayList<String> brandListId;
        ArrayList<String> modelListId;
    
        String brandid;
        String modelId;
        ArrayList<HashMap<String, String>> addVehiclelist;
    
        String jsonStr;
        String Regno, fuelType, Brand, Year, Model, variant;
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            brandListArray = new ArrayList<String>();
            brandListId = new ArrayList<String>();
            modelListArray = new ArrayList<String>();
            modelListId = new ArrayList<String>();
    
    
            addVehiclelist = new ArrayList<HashMap<String, String>>();
            petrol = (RadioButton) findViewById(R.id.petrol);
            Diesel = (RadioButton) findViewById(R.id.diesel);
            fullyloaded = (RadioButton) findViewById(R.id.fullyLoaded);
            Basicmodel = (RadioButton) findViewById(R.id.basicmodel);
            addVehicleBtn = (Button) findViewById(R.id.addVehicleBtn);
            AddVehicle_Regno = (EditText) findViewById(R.id.AddVehicle_Regno);
            kmscovered = (EditText) findViewById(R.id.kmsCovered);
            textView8 = (TextView) findViewById(R.id.textView8);
            textViewkms = (TextView) findViewById(R.id.textViewkms);
            AddspinnerMake = (Spinner) findViewById(R.id.AddspinnerMake);
            AddspinnerModel = (Spinner) findViewById(R.id.AddspinnerModel);
            AddspinnerYear = (Spinner) findViewById(R.id.AddspinnerYear);
    
    
    
            new Getbrands().execute();
    
            petrol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                    if (petrol.isChecked()) {
                        fuelType = petrol.getText().toString();
                    }
                }
            });
    
            Diesel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                    if (Diesel.isChecked()) {
                        fuelType = Diesel.getText().toString();
                    }
                }
            });
            AddspinnerMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                    Brand = AddspinnerMake.getSelectedItem().toString();
    
    
                    if (position > 0) {
                        brandid = brandListId.get(position).toString();
    
    
                        //**Get Model execute api//
                        new Getmodels().execute();
                    }
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
            AddspinnerYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    Year = AddspinnerYear.getSelectedItem().toString();
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
            AddspinnerModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                    Model = AddspinnerModel.getSelectedItem().toString();
    
                    if (position > 0) {
                        modelId = modelListId.get(position).toString();
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
            Basicmodel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                    if (Basicmodel.isChecked()) {
                        variant = Basicmodel.getText().toString();
                    }
                }
            });
    
            fullyloaded.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                    if (fullyloaded.isChecked()) {
                        variant = fullyloaded.getText().toString();
                    }
                }
            });
    
    
    
            addVehicleBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Regno = AddVehicle_Regno.getText().toString();
                    new GetVehicle().execute();
    
    
                    Intent intent = new Intent(MainActivity.this, AddVehicle.class);
                    startActivity(intent);
                }
            });
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
    
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        public Action getIndexApiAction() {
            Thing object = new Thing.Builder()
                    .setName("Main Page") // TODO: Define a title for the content shown.
                    // TODO: Make sure this auto-generated URL is correct.
                    .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                    .build();
            return new Action.Builder(Action.TYPE_VIEW)
                    .setObject(object)
                    .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                    .build();
        }
    
        @Override
        public void onStart() {
            super.onStart();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client.connect();
            AppIndex.AppIndexApi.start(client, getIndexApiAction());
        }
    
        @Override
        public void onStop() {
            super.onStop();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            AppIndex.AppIndexApi.end(client, getIndexApiAction());
            client.disconnect();
        }
    
        private class GetVehicle extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
    
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://garage.kaptastech.mobi/api/5k/master/vehicle");
    
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
                nameValuePair.add(new BasicNameValuePair("user_id","5"));
                nameValuePair.add(new BasicNameValuePair("registration_no", Regno));
                nameValuePair.add(new BasicNameValuePair("brand","5"));
                nameValuePair.add(new BasicNameValuePair("model", "4"));
                nameValuePair.add(new BasicNameValuePair("type", "2"));
                nameValuePair.add(new BasicNameValuePair("variant", "2"));
                nameValuePair.add(new BasicNameValuePair("year", Year));
    
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                } catch (UnsupportedEncodingException e) {
                    // writing error to Log
                    e.printStackTrace();
                }
    
                // Making HTTP Request
                try {
                    HttpResponse response = client.execute(httpPost);
    
    
                    // writing response to log
                    Log.d("Response from url:", response.toString());
                    jsonStr = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    /*HttpConnection ht = new HttpConnection();
                    ht.getVehicle();*/
                    JSONObject object = new JSONObject(jsonStr);
                    JSONArray Vehicle = object.getJSONArray("data");
                    for (int i = 0; i < Vehicle.length(); i++) {
    
                        JSONObject c = Vehicle.getJSONObject(i);
                        String Regno = c.getString("registration_no");
                        String brand = c.getString("brand");
                        String model = c.getString("model");
                        String fueltype = c.getString("type");
                        String variant = c.getString("variant");
                        HashMap<String, String> Vl = new HashMap<String, String>();
                        Vl.put("registration_no", Regno);
                        Vl.put("brand", brand);
                        Vl.put("model", model);
                        Vl.put("type", fueltype);
                        Vl.put("variant:", variant);
                        addVehiclelist.add(Vl);
                        values = new String[]{
                                "Registration No:" + Regno,
                                "Brand:" + brand,
                                "Model:" + model,
                                "Type:" + fueltype,
                                "Variant:" + variant,
                        };
                    }
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
            }
        }
    
        private class Getbrands extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://garage.kaptastech.mobi/api/5k/master/brand");
    
                try {
                    HttpResponse response = client.execute(httpGet);
    
    
                    // writing response to log
                    Log.d("Response from url:", response.toString());
                    jsonStr = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    HttpConnection ht = new HttpConnection();
                    ht.getbrand();
                    JSONObject object = new JSONObject(jsonStr);
                    JSONArray brandArray = object.getJSONArray("data");
    
                    brandListArray.clear();
                    brandListId.clear();
                    brandListArray.add("Select Brand");
                    brandListId.add("0");
                    for (int i = 0; i < brandArray.length(); i++) {
                        JSONObject gb = brandArray.getJSONObject(i);
                        brandListArray.add(gb.getString("name"));
                        brandListId.add(gb.getString("id"));
                        String[] brandStringArray = new String[brandListArray.size()];
                        brandStringArray = brandListArray.toArray(brandStringArray);
                        for (int i1 = 0; i1 < brandStringArray.length; i1++) {
                            Log.d("String is", (String) brandStringArray[i1]);
                        }
    
                    }
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                return null;
            }
    
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
    
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, brandListArray);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                AddspinnerMake.setAdapter(adapter);
    
    
            }
        }
    
        private class Getmodels extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://garage.kaptastech.mobi/api/5k/master/models/" + brandid);
    
                try {
                    HttpResponse response = client.execute(httpGet);
    
    
                    // writing response to log
                    Log.d("Response from url:", response.toString());
                    jsonStr = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    HttpConnection ht = new HttpConnection();
                    ht.getmodel();
                    JSONObject object = new JSONObject(jsonStr);
                    JSONArray modelArray = object.getJSONArray("data");
    
                    modelListArray.clear();
                    modelListId.clear();
                    modelListArray.add("Select Model");
                    modelListId.add("0");
                    for (int i = 0; i < modelArray.length(); i++) {
                        JSONObject gm = modelArray.getJSONObject(i);
                        modelListArray.add(gm.getString("name"));
                        modelListId.add(gm.getString("id"));
                        String[] modelStringArray = new String[modelListArray.size()];
                        modelStringArray = modelListArray.toArray(modelStringArray);
                        for (int i2 = 0; i2 < modelStringArray.length; i2++) {
                            Log.d("String is", (String) modelStringArray[i2]);
                        }
                    }
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                return null;
            }
    
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, modelListArray);
                AddspinnerModel.setAdapter(adapter);
    
    
            }
        }
    }
    
    
    
    
    
    
    
    AddVehicle.java(My 2nd activity)
    package com.example.addvehicle;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.AsyncTask;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    public class AddVehicle extends AppCompatActivity {
        ListView addVehicleListView;
        ArrayList<HashMap<String, String>> VehicleList;
        ArrayList<String> brandListId;
        private String[] values;
        private static String urlString = "http://garage.kaptastech.mobi/api/5k/users/vehicle";
        private String TAG = AddVehicle.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);
            brandListId = new ArrayList<String>();
            VehicleList = new ArrayList<HashMap<String, String>>();
            addVehicleListView = (ListView) findViewById(R.id.addVehicleListView);
            new GetVehicle().execute();
        }
    
        private class GetVehicle extends AsyncTask<Void, Void, Void> {
    
    
            @Override
            protected Void doInBackground(Void... voids) {
    
                HttpConnection ht = new HttpConnection();
                String response = ht.getVehicle();
                {
                    try {
    
                        HttpClient client = new DefaultHttpClient();
                        HttpGet get = new HttpGet("http://garage.kaptastech.mobi/api/5k/users/vehicle");
                        JSONObject Object = new JSONObject(response);
                        JSONArray Vehicle = Object.getJSONArray("data");
                        for (int i = 0; i < Vehicle.length(); i++) {
                            JSONObject c = Vehicle.getJSONObject(i);
                            String Regno = c.getString("registration_no");
                            String brand = c.getString("brand_id");
                            String model = c.getString("model_id");
                            String fueltype = c.getString("type");
                            String variant = c.getString("variant");
                            HashMap<String, String> Vl = new HashMap<String, String>();
                            Vl.put("registration_no", Regno);
                            Vl.put("brand_id", brand);
                            Vl.put("model_id", model);
                            Vl.put("type", fueltype);
                            Vl.put("variant:" , variant);
                            VehicleList.add(Vl);
                            values = new String[]{
                                    "Registration No:" + Regno,
                                    "Brand Id:" + brand,
                                    "Model Id:" + model,
                                    "Type:" + fueltype,
                                    "Variant:" + variant,
                            };
                        }
                    } catch (final Exception e) {
                        e.printStackTrace();
                    }
                }
                return null;
    
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
    
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddVehicle.this,
                        android.R.layout.simple_list_item_1, android.R.id.text1, values);
                //ListAdapter adapter = new SimpleAdapter(AddVehicle.this, VehicleList, R.layout.activity_list, new String[]{"brand_id", "model_id", "registration_no", "type", "variant"}, new int[]{R.id.AddspinnerMake, R.id.AddspinnerModel, R.id.AddVehicle_Regno, R.id.fullyLoaded, R.id.basicmodel});
                addVehicleListView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
    
    
            }
        }
    }
    
    
    
    
    
    
    
    
    

     

     

     

     

     

     

                                                         

    How can i arrive at the my expected output in my android project?

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: