Search code examples
androidxmlapiarabic

API calling when phone local language is Arabic in android


I am working on an android project, in which i have to call APIs in english when mobile local language is Arabic. i am not getting API URl String in English. My API calling file(PackageapiCall.class) is as follows:

package com.example.app.roamer.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.app.roamer.Constant;
import com.example.app.roamer.R;
import com.example.app.roamer.activities.SplashScreen;
import com.example.app.roamer.app.AppController;
import com.example.app.roamer.fragments.AlltripsFragment;
import com.example.app.roamer.helper.DateFormatsHelper;
import com.example.app.roamer.fragments.TaTripsFragment;
import com.example.app.roamer.models.AllTrips;
import com.example.app.roamer.tasks.GenerateTokenOperation;
import com.example.app.roamer.tasks.ParsingOperation;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.example.app.roamer.activities.HomeActivity.TAG;

/**
 * Created by manish on 22-12-2016.
 */

public class PackageApicalls {
    private Request.Priority priority = Request.Priority.HIGH;
    Context ctx;
    private List<AllTrips.TripsBean> allTripsByMobileUserIDNewData, allTripsByMobileUserIDNewData1;
    AlltripsFragment alltripsFragment;
    TaTripsFragment taTripsFragment;
    Boolean splashScreenApiCall=false;
     SharedPreferences sharedPreferences;
    private int mobileUserId;

    public PackageApicalls(Context ctx, AlltripsFragment alltripsFragment) {
        this.ctx=ctx;
        this.alltripsFragment=alltripsFragment;
    }
    public PackageApicalls(Context ctx, TaTripsFragment taTripsFragment) {
        this.ctx=ctx;
        this.taTripsFragment=taTripsFragment;
    }
    public PackageApicalls(Context ctx) {
        this.ctx=ctx;
    }


    public void getalltripdetails() {

     //  String gettodaysdate="02-02-2016";
        DateFormatsHelper dateFormatsHelper = new DateFormatsHelper();
        String gettodaysdate = dateFormatsHelper.gettodaysdate();
        sharedPreferences = ctx.getSharedPreferences(Constant.MyPREFERENCES, Context.MODE_PRIVATE);
         mobileUserId = sharedPreferences.getInt("mobileUserId", 0);

       //mobileUserId=1;
        String url = ctx.getResources().getString(R.string.alltrips_api_url,mobileUserId,gettodaysdate);
        Log.d(TAG, "manish-url-packageapicalls"+url);
        StringRequest postRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (Looper.myLooper() == Looper.getMainLooper()) {
                            Log.d(TAG, "main thread-true");
                        } else {
                            Log.d(TAG, "main thread-false");
                        }
                        // response
                        Log.d("Response", response);

                        allTripsByMobileUserIDNewData = parsejson(response);
//                          setupui(allTripsByMobileUserIDNewData);
                        Log.d("manishbutola", "onResponse: New Data arrived");
                        // pDialog.hide();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("manishbutola", "getalltripdetailserror => " + error.toString());
                        new GenerateTokenOperation(ctx).execute();
                      if(new GenerateTokenOperation(ctx).getStatus()==  AsyncTask.Status.FINISHED){
                          getalltripdetails();
                      }
                        /*if(GenerateTokenOperation(ctx).Status == AsyncTask.Status.FINISHED){
                            // START NEW TASK HERE
                        }*/


                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                sharedPreferences = ctx.getSharedPreferences(Constant.MyPREFERENCES, Context.MODE_PRIVATE);
                String token_value = sharedPreferences.getString("token", null);
                String authorizedtoken = "bearer" + " " +token_value;
                //params.put("User-Agent", "Nintendo Gameboy");
                Log.d("packageApiCalls", "authorizedtoken: "+ authorizedtoken);
                params.put("Authorization", authorizedtoken);
                return params;
            }

            @Override
            public Priority getPriority() {
                return priority;
            }
        };
        AppController.getInstance().addToRequestQueue(postRequest);
        //return parsejson;
        // List<AllTrips.TripsBean>  allTrips=db.getAllTripsByMobileUserID(1);
    }
    private List<AllTrips.TripsBean> parsejson(String response) {
        try {
            if(Constant.splashScreenApiCall){
                new ParsingOperation(ctx).execute(response);

            }
            else{
                new ParsingOperation(ctx,alltripsFragment).execute(response);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return allTripsByMobileUserIDNewData1;
    }

}

My `gettodaydate` method as follows:

public String gettodaysdate(){
        DateFormat formatter;
        formatter = new SimpleDateFormat("MM-dd-yyyy",Locale.ENGLISH);
        String result = formatter.format(new Date());

        /*DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        Date date = new Date();*/
//        return dateFormat.format(date);
        return result;
    }

and finally my string.xml :

<string name="alltrips_api_ta_url" translatable="false">http://roamer.techmaster.in/Rest/api/GetAgentTrips?agentId="%1$d"&amp;isPreviousTrip=false&amp;date="%2$s"</string>

I ma getting log cat as:

Unexpected response code 400 for http://roamer.techmaster.in/Rest/api/GetTrips?mobileUserId=١&isPreviousTrip=false&date=02-14-2017

as i am seeing that mobileUserId is 1 and it is going to convert in url in Arabic when url going to hit to server. app going to crash.Please help me out.Thanks.


Solution

  • After struggle i got an idea and it worked, it is: i change int to string like:
    String userId = String.valueOf(mobileUserId); and then i do <string name="alltrips_api_ta_url" translatable="false">http://base_url/trips?agentId="%1$d"&amp;isPreviousTrip=false&amp;date="%2$s"</string> to

    <string name="alltrips_api_ta_url" translatable="false">http://base_url/GetAgentTrips?agentId="%1$s"&amp;isPreviousTrip=false&amp;date="%2$s"</string>
    

    and then i put

    String url = ctx.getResources().getString(R.string.alltrips_api_url,userId   ,gettodaysdate);